This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27306-jolt-jsonnode in repository https://gitbox.apache.org/repos/asf/camel.git
commit 777e470007d912708b41ec96559e2fa668d6519d Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 20 19:25:50 2026 +0100 CAMEL-27306: camel-jolt - a Jackson JsonNode body is converted to the Map / List tree Jolt needs With the default inputType (Hydrated), a Jackson JsonNode body (Jackson 3, or Jackson 2 detected by its package as Jackson 2 is no dependency) reached the Jolt transform as an object Jolt does not know: the spec matched nothing and the result was null, with no error. Such a body comes from the application-x-struct data type, or the JSON data format with unmarshalType JsonNode (as in the json-deserialize-action Kamelet). A JsonNode body is now parsed from its JSON text with Jolt's JsonUtils, with either inputType, so the transform gets the same tree as for the same JSON as text, with the same number types. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../org/apache/camel/catalog/components/jolt.json | 2 +- .../apache/camel/catalog/docs/jolt-component.adoc | 4 +- components/camel-jolt/pom.xml | 6 +++ .../org/apache/camel/component/jolt/jolt.json | 2 +- .../camel-jolt/src/main/docs/jolt-component.adoc | 4 +- .../apache/camel/component/jolt/JoltEndpoint.java | 26 +++++++++++-- .../component/jolt/JoltHydratedInputTest.java | 43 +++++++++++++++++++++- 7 files changed, 78 insertions(+), 9 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jolt.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jolt.json index 4b7a1d1a32df..3afafd3bc32a 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jolt.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jolt.json @@ -35,7 +35,7 @@ "CamelJoltContext": { "index": 1, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Map<String, Object>", "deprecated": false, "autowired": false, "secret": false, "description": "The context", "constantName": "org.apache.camel.component.jolt.JoltConstants#JOLT_CONTEXT" } }, "bodyTypes": { - "producerIn": { "carriers": [ "map", "list", "text", "bytes", "stream", "file" ], "format": "json", "byOption": [ { "option": "inputType", "value": "JsonString", "carriers": [ "text", "bytes", "stream", "file" ] } ], "description": "Parsed JSON (a Map or a List), or JSON as text, bytes, a stream or a file, which is parsed. Text that does not start with { or [ reaches the spec as it is. With inputType=JsonString the body is always parsed as JSON" }, + "producerIn": { "carriers": [ "map", "list", "text", "bytes", "stream", "file" ], "format": "json", "byOption": [ { "option": "inputType", "value": "JsonString", "carriers": [ "text", "bytes", "stream", "file" ] } ], "description": "Parsed JSON (a Map or a List), or JSON as text, bytes, a stream, a file or a Jackson JsonNode, which is parsed. Text that does not start with { or [ reaches the spec as it is. With inputType=JsonString the body is always parsed as JSON" }, "producerOut": { "carriers": [ "map", "list" ], "format": "json", "byOption": [ { "option": "outputType", "value": "JsonString", "carriers": [ "text" ], "javaType": "java.lang.String" } ], "description": "The JSON the spec makes: a Map for an object, a List for an array; JSON text with outputType=JsonString" } }, "properties": { diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jolt-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jolt-component.adoc index bccc7bb661cf..8da1ab42fb52 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jolt-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jolt-component.adoc @@ -57,12 +57,14 @@ With the default `inputType`, `Hydrated`, the body can be: * bytes, an `InputStream` (including a stream cache) or a file (`java.io.File`, `java.nio.file.Path` or the file of the xref:file-component.adoc[File] component), which is parsed as JSON. The text is decoded in the charset of the exchange (`Exchange.CHARSET_NAME`) when one is set; otherwise the JSON parser detects the encoding; +* a Jackson `JsonNode` (Jackson 3 or Jackson 2), such as the body of the `application-x-struct` data type or of the +JSON data format with `unmarshalType` set to `JsonNode`, which is converted to a `Map` or a `List` from its JSON text; * a `Map` or a `List` (hydrated JSON), which is used as it is. Text that does not start with `{` or `[` is passed to the specification as it is. Text that starts with `{` or `[` but is not valid JSON fails with the parse error. -With `inputType=JsonString`, the body is always parsed as JSON. +With `inputType=JsonString`, the body is always parsed as JSON, and a Jackson `JsonNode` is converted the same way. The output is hydrated JSON (a `Map` or a `List`) by default; set `outputType=JsonString` to get a JSON `String`. diff --git a/components/camel-jolt/pom.xml b/components/camel-jolt/pom.xml index c0477581726f..caa78bddc3d8 100644 --- a/components/camel-jolt/pom.xml +++ b/components/camel-jolt/pom.xml @@ -67,6 +67,12 @@ <artifactId>camel-test-junit6</artifactId> <scope>test</scope> </dependency> + <!-- a body can be a Jackson 2 node --> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/components/camel-jolt/src/generated/resources/META-INF/org/apache/camel/component/jolt/jolt.json b/components/camel-jolt/src/generated/resources/META-INF/org/apache/camel/component/jolt/jolt.json index 4b7a1d1a32df..3afafd3bc32a 100644 --- a/components/camel-jolt/src/generated/resources/META-INF/org/apache/camel/component/jolt/jolt.json +++ b/components/camel-jolt/src/generated/resources/META-INF/org/apache/camel/component/jolt/jolt.json @@ -35,7 +35,7 @@ "CamelJoltContext": { "index": 1, "kind": "header", "displayName": "", "group": "producer", "label": "", "required": false, "javaType": "Map<String, Object>", "deprecated": false, "autowired": false, "secret": false, "description": "The context", "constantName": "org.apache.camel.component.jolt.JoltConstants#JOLT_CONTEXT" } }, "bodyTypes": { - "producerIn": { "carriers": [ "map", "list", "text", "bytes", "stream", "file" ], "format": "json", "byOption": [ { "option": "inputType", "value": "JsonString", "carriers": [ "text", "bytes", "stream", "file" ] } ], "description": "Parsed JSON (a Map or a List), or JSON as text, bytes, a stream or a file, which is parsed. Text that does not start with { or [ reaches the spec as it is. With inputType=JsonString the body is always parsed as JSON" }, + "producerIn": { "carriers": [ "map", "list", "text", "bytes", "stream", "file" ], "format": "json", "byOption": [ { "option": "inputType", "value": "JsonString", "carriers": [ "text", "bytes", "stream", "file" ] } ], "description": "Parsed JSON (a Map or a List), or JSON as text, bytes, a stream, a file or a Jackson JsonNode, which is parsed. Text that does not start with { or [ reaches the spec as it is. With inputType=JsonString the body is always parsed as JSON" }, "producerOut": { "carriers": [ "map", "list" ], "format": "json", "byOption": [ { "option": "outputType", "value": "JsonString", "carriers": [ "text" ], "javaType": "java.lang.String" } ], "description": "The JSON the spec makes: a Map for an object, a List for an array; JSON text with outputType=JsonString" } }, "properties": { diff --git a/components/camel-jolt/src/main/docs/jolt-component.adoc b/components/camel-jolt/src/main/docs/jolt-component.adoc index bccc7bb661cf..8da1ab42fb52 100644 --- a/components/camel-jolt/src/main/docs/jolt-component.adoc +++ b/components/camel-jolt/src/main/docs/jolt-component.adoc @@ -57,12 +57,14 @@ With the default `inputType`, `Hydrated`, the body can be: * bytes, an `InputStream` (including a stream cache) or a file (`java.io.File`, `java.nio.file.Path` or the file of the xref:file-component.adoc[File] component), which is parsed as JSON. The text is decoded in the charset of the exchange (`Exchange.CHARSET_NAME`) when one is set; otherwise the JSON parser detects the encoding; +* a Jackson `JsonNode` (Jackson 3 or Jackson 2), such as the body of the `application-x-struct` data type or of the +JSON data format with `unmarshalType` set to `JsonNode`, which is converted to a `Map` or a `List` from its JSON text; * a `Map` or a `List` (hydrated JSON), which is used as it is. Text that does not start with `{` or `[` is passed to the specification as it is. Text that starts with `{` or `[` but is not valid JSON fails with the parse error. -With `inputType=JsonString`, the body is always parsed as JSON. +With `inputType=JsonString`, the body is always parsed as JSON, and a Jackson `JsonNode` is converted the same way. The output is hydrated JSON (a `Map` or a `List`) by default; set `outputType=JsonString` to get a JSON `String`. diff --git a/components/camel-jolt/src/main/java/org/apache/camel/component/jolt/JoltEndpoint.java b/components/camel-jolt/src/main/java/org/apache/camel/component/jolt/JoltEndpoint.java index 09121b5344c2..31c22ebe8185 100644 --- a/components/camel-jolt/src/main/java/org/apache/camel/component/jolt/JoltEndpoint.java +++ b/components/camel-jolt/src/main/java/org/apache/camel/component/jolt/JoltEndpoint.java @@ -44,6 +44,7 @@ import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.support.ExchangeHelper; import org.apache.camel.util.ObjectHelper; +import tools.jackson.databind.JsonNode; /** * JSON to JSON transformation using JOLT. @@ -58,9 +59,10 @@ import org.apache.camel.util.ObjectHelper; carriers = { Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE }), - description = "Parsed JSON (a Map or a List), or JSON as text, bytes, a stream or a file," - + " which is parsed. Text that does not start with { or [ reaches the spec as" - + " it is. With inputType=JsonString the body is always parsed as JSON"), + description = "Parsed JSON (a Map or a List), or JSON as text, bytes, a stream, a file or a" + + " Jackson JsonNode, which is parsed. Text that does not start with { or [" + + " reaches the spec as it is. With inputType=JsonString the body is always" + + " parsed as JSON"), producerOut = @BodyType(value = { Carrier.MAP, Carrier.LIST }, format = "json", byOption = @BodyType.When(option = "outputType", value = "JsonString", carriers = Carrier.TEXT, javaType = "java.lang.String"), @@ -68,6 +70,9 @@ import org.apache.camel.util.ObjectHelper; + " with outputType=JsonString")) public class JoltEndpoint extends ResourceEndpoint { + // the package of the Jackson 2 nodes (ObjectNode, ArrayNode, ...), checked by name as Jackson 2 is no dependency + private static final String JACKSON2_NODE_PACKAGE = "com.fasterxml.jackson.databind.node."; + private JoltTransform transform; @UriParam(defaultValue = "Hydrated") @@ -230,7 +235,12 @@ public class JoltEndpoint extends ResourceEndpoint { } Object input; - if (getInputType() == JoltInputOutputType.JsonString) { + Object in = exchange.getIn().getBody(); + if (isJsonNode(in)) { + // Jolt only works on Map and List trees, so a Jackson node is parsed from its JSON text, + // which gives the same tree as the same JSON given as text + input = JsonUtils.jsonToObject(in.toString()); + } else if (getInputType() == JoltInputOutputType.JsonString) { input = JsonUtils.jsonToObject(exchange.getIn().getBody(InputStream.class)); } else { input = hydratedInput(exchange); @@ -276,6 +286,14 @@ public class JoltEndpoint extends ResourceEndpoint { return body; } + /** + * Whether the body is a Jackson 3 or Jackson 2 JsonNode, whose toString() is its JSON text + */ + private static boolean isJsonNode(Object body) { + return body instanceof JsonNode + || body != null && body.getClass().getName().startsWith(JACKSON2_NODE_PACKAGE); + } + /** * Whether the text is a JSON object or array, that is whether its first non-whitespace character is { or [ */ diff --git a/components/camel-jolt/src/test/java/org/apache/camel/component/jolt/JoltHydratedInputTest.java b/components/camel-jolt/src/test/java/org/apache/camel/component/jolt/JoltHydratedInputTest.java index 6c434b3fa462..1db409ae0941 100644 --- a/components/camel-jolt/src/test/java/org/apache/camel/component/jolt/JoltHydratedInputTest.java +++ b/components/camel-jolt/src/test/java/org/apache/camel/component/jolt/JoltHydratedInputTest.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; +import com.fasterxml.jackson.databind.ObjectMapper; import io.joltcommunity.jolt.exception.JsonUnmarshalException; import org.apache.camel.CamelExecutionException; import org.apache.camel.Exchange; @@ -32,17 +33,22 @@ import org.apache.camel.converter.stream.InputStreamCache; import org.apache.camel.test.junit6.CamelTestSupport; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import tools.jackson.databind.json.JsonMapper; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; /** - * The default inputType (Hydrated) parses a body that is JSON text, bytes, a stream or a file. + * The default inputType (Hydrated) parses a body that is JSON text, bytes, a stream, a file or a Jackson JsonNode. */ public class JoltHydratedInputTest extends CamelTestSupport { private static final String JSON = "{\"name\": \"Camel\"}"; + // numbers, nesting and an array, to compare a node with the same JSON as text + private static final String NESTED_JSON + = "{\"name\": \"Camel\", \"age\": 19, \"rating\": 4.5, \"big\": 12345678901234," + + " \"tags\": [\"integration\", \"eip\"], \"active\": true, \"owner\": {\"org\": \"ASF\"}, \"none\": null}"; @TempDir Path tempDir; @@ -107,6 +113,39 @@ public class JoltHydratedInputTest extends CamelTestSupport { assertFullName("Camel", template.requestBody("direct:shiftr", Map.of("name", "Camel"))); } + @Test + public void testJackson3JsonNode() { + assertFullName("Camel", template.requestBody("direct:shiftr", JsonMapper.shared().readTree(JSON))); + } + + @Test + public void testJackson2JsonNode() throws Exception { + assertFullName("Camel", template.requestBody("direct:shiftr", new ObjectMapper().readTree(JSON))); + } + + @Test + public void testJackson3ArrayNode() { + Object out = template.requestBody("direct:sortr", JsonMapper.shared().readTree("[\"b\", \"a\"]")); + assertEquals(List.of("b", "a"), assertInstanceOf(List.class, out)); + } + + @Test + public void testJackson2ArrayNode() throws Exception { + Object out = template.requestBody("direct:sortr", new ObjectMapper().readTree("[\"b\", \"a\"]")); + assertEquals(List.of("b", "a"), assertInstanceOf(List.class, out)); + } + + @Test + public void testJsonNodeGivesTheSameResultAsJsonText() throws Exception { + // the identity spec (Sortr) shows the tree that Jolt gets, with the number types of the parsed JSON + Object fromText = template.requestBody("direct:sortr", NESTED_JSON); + assertInstanceOf(Map.class, fromText); + assertEquals(fromText, template.requestBody("direct:sortr", JsonMapper.shared().readTree(NESTED_JSON))); + assertEquals(fromText, template.requestBody("direct:sortr", new ObjectMapper().readTree(NESTED_JSON))); + assertEquals(fromText, template.requestBody("direct:sortr-string", JsonMapper.shared().readTree(NESTED_JSON))); + assertEquals(fromText, template.requestBody("direct:sortr-string", new ObjectMapper().readTree(NESTED_JSON))); + } + @Test public void testTextThatIsNotJsonIsKept() { assertEquals("hello", template.requestBody("direct:sortr", "hello")); @@ -132,6 +171,8 @@ public class JoltHydratedInputTest extends CamelTestSupport { .to("jolt:org/apache/camel/component/jolt/shiftr.json?transformDsl=Shiftr"); from("direct:sortr") .to("jolt:dummy?transformDsl=Sortr"); + from("direct:sortr-string") + .to("jolt:dummy?transformDsl=Sortr&inputType=JsonString"); } }; }
