Croway commented on code in PR #30376:
URL: https://github.com/apache/camel/pull/30376#discussion_r4626785322


##########
components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java:
##########
@@ -195,19 +196,26 @@ public Object evaluate(Exchange exchange) {
      * @return          the {@link JsonNode} to be processed by the expression
      */
     private JsonNode getPayload(Exchange exchange) throws Exception {
-        JsonNode payload = source.evaluate(exchange, JsonNode.class);
+        Object value = source.evaluate(exchange, Object.class);
+        // Jackson 3 cannot convert a Jackson 2 node; the text of a node of 
either line is its JSON
+        JsonNode payload = typeConverter.convertTo(JsonNode.class, exchange,
+                isJackson2Node(value) ? value.toString() : value);
         // if body is stream cached then reset, so we can re-read it again
         MessageHelper.resetStreamCache(exchange.getMessage());
         if (payload == null) {
             // tell apart a source that is null from one that is set but holds 
no JSON
-            if (source.evaluate(exchange, Object.class) == null) {
+            if (value == null) {
                 throw new InvalidPayloadException(exchange, JsonNode.class, 
exchange.getIn(), nullSourceHint(exchange));
             }
             throw new InvalidPayloadException(exchange, JsonNode.class);
         }
         return payload;
     }
 
+    private static boolean isJackson2Node(Object value) {

Review Comment:
   **A Jackson 2 `resultType` now gives `null` silently.** A Jackson 2 node 
goes in through its JSON text, but there is no such path out: `.jq(".g", 
com.fasterxml.jackson.databind.JsonNode.class)`, or 
`resultType="com.fasterxml.jackson.databind.JsonNode"`, returns `null`, even 
with `camel-jackson` on the classpath. No converter goes from a `tools.jackson` 
node to a Jackson 2 node, and `convertTo` returns `null` instead of failing. 
Main returned `{"x":1}`. The route still compiles whenever something else 
brings Jackson 2 (`camel-jackson` for example), so this only shows at runtime.
   
   Going through the JSON text, as for the input, gives the node back (checked 
with `camel-jackson` on the test classpath; the camel-jq tests still pass):
   
   ```java
   private Object convertResult(Exchange exchange, JsonNode node) {
       // Jackson 3 cannot convert to a Jackson 2 node: go through its JSON 
text, as for the input
       Object value = 
resultType.getName().startsWith("com.fasterxml.jackson.databind.") ? 
node.toString() : node;
       return this.typeConverter.convertTo(resultType, exchange, value);
   }
   ```
   
   in place of the two `typeConverter.convertTo(resultType, ...)` calls in 
`evaluate`. Failing in `init` with a clear message would do too. Either way, a 
line in the upgrade guide would help.
   
   _Claude Code on behalf of Croway_



##########
components/camel-jq/src/main/java/org/apache/camel/language/jq/JqExpression.java:
##########
@@ -195,19 +196,26 @@ public Object evaluate(Exchange exchange) {
      * @return          the {@link JsonNode} to be processed by the expression
      */
     private JsonNode getPayload(Exchange exchange) throws Exception {
-        JsonNode payload = source.evaluate(exchange, JsonNode.class);
+        Object value = source.evaluate(exchange, Object.class);
+        // Jackson 3 cannot convert a Jackson 2 node; the text of a node of 
either line is its JSON
+        JsonNode payload = typeConverter.convertTo(JsonNode.class, exchange,

Review Comment:
   **An empty or blank body regresses.** For `""`, `"   "` or `"\n"`, the 
`readTree` of Jackson 3 returns a `MissingNode`, and the Jackson 3 provider of 
jackson-jq 2 does not know that node type:
   
   | Expression | main | this PR |
   |---|---|---|
   | `type` | `"null"` | `IllegalStateException: Unknown JsonNodeType: MISSING` 
|
   | `try .a catch "c"` | `"c"` | the same `IllegalStateException`: jq's `try` 
cannot catch it |
   | `. // "def"` | `"def"` | the `MissingNode` itself: the alternative is not 
taken |
   
   An empty file or an HTTP request without content gets there. Reproducer: 
`from("direct:start").transform().jq("type")` with 
`template.requestBody("direct:start", "")`.
   
   Reading it as `null`, as jackson-jq 1 did, gives `"null"`, `null` (as jq 
does for `null | .a`) and `"def"`. The camel-jq tests still pass with:
   
   ```java
   JsonNode payload = typeConverter.convertTo(JsonNode.class, exchange,
           isJackson2Node(value) ? value.toString() : value);
   if (payload != null && payload.isMissingNode()) {
       // an empty or blank source holds no JSON value: read it as null, as 
jackson-jq 1 did
       payload = NullNode.getInstance();
   }
   ```
   
   _Claude Code on behalf of Croway_



##########
components/camel-jq/src/main/docs/jq-language.adoc:
##########
@@ -22,8 +22,8 @@ include::partial$language-options.adoc[]
 
 === Message body types
 
-Camel JQ leverages `camel-jackson` for type conversion.
-To enable camel-jackson POJO type conversion, refer to the Camel Jackson 
documentation.
+Camel JQ leverages `camel-jackson3` (Jackson 3) for type conversion.

Review Comment:
   **Spring Boot: jq now uses the application's `JsonMapper`.** The converters 
of `camel-jackson3` use the `tools.jackson` `ObjectMapper` of the registry when 
there is exactly one. In a Spring Boot 4 app with `spring-boot-starter-jackson` 
(any web app), that is the `JsonMapper` of Spring Boot, so the 
`spring.jackson.*` settings now apply to how jq reads its input and how its 
results are converted. For example, with 
`spring.jackson.json.read.allow-single-quotes=true`, a body `{'a':1}` is 
accepted by jq with this PR and rejected on main. That is probably what users 
want, but it is worth a sentence here or in the upgrade guide.
   
   _Claude Code on behalf of Croway_



##########
components/camel-jq/pom.xml:
##########
@@ -43,16 +43,41 @@
         </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
-            <artifactId>camel-jackson</artifactId>
+            <artifactId>camel-jackson3</artifactId>

Review Comment:
   **Question: are you OK with `marshal().json()` switching to Jackson 3 in an 
application that also has `camel-jackson`?**
   
   camel-jq now brings `camel-jackson3`. When `camel-jackson` is on the 
classpath too, `DefaultDataFormatResolver` prefers the Jackson 3 `jackson` data 
format, so upgrading camel-jq alone changes how the rest of the application 
writes JSON. Among the components and DSLs, only camel-jq, camel-kamelet, 
camel-kamelet-main and camel-jackson3-avro/-protobuf bring `camel-jackson3` at 
compile scope.
   
   In a Spring Boot 4.1.2 app with `camel-jackson-starter`, `camel-jq-starter` 
and `camel.dataformat.jackson.include=NON_NULL`, the same POJO is written as:
   
   - main: `{"name":"Bob","age":3,"when":0}`
   - this PR: 
`{"age":3,"name":"Bob","note":null,"when":"1970-01-01T00:00:00.000Z"}`
   
   The Jackson 3 defaults apply (alphabetical order, ISO dates), and the 
`camel.dataformat.jackson.*` properties are no longer applied: the customizer 
of `camel-jackson-starter` only targets the Jackson 2 `JacksonDataFormat`, and 
there is no `camel-jackson3-starter`.
   
   A related effect: both lines read the same global options 
(`CamelJacksonEnableTypeConverter`, `CamelJacksonTypeConverterToPojo`). With 
`camel-jackson` added to the camel-jq test classpath, `JqExpressionPojoTest` 
fails: the Jackson 2 fallback converter takes the `tools.jackson` node and 
reads it as a bean (`Unrecognized field "nodeType" (class 
...JqExpressionPojoTest$Book)`). Without `camel-jackson` it passes, and on main 
that classpath is the usual one.
   
   If this is intended, could the camel-jq section of the upgrade guide say so, 
and what to do about it? Otherwise, camel-jq could read its input with its own 
Jackson 3 mapper and not bring `camel-jackson3`, though the conversions of the 
results (to `String`, to a POJO) come from `camel-jackson3` today.
   
   _Claude Code on behalf of Croway_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to