This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27270 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 8b50566ee91f70cdb32d92d561a9a78da0d87964 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 20 07:48:57 2026 +0100 CAMEL-27270: camel-jackson and camel-jackson3 - application-json keeps a Json value given as text when the Content-Type is application/json Since CAMEL-26300 application-json keeps text that is a Json object or array and writes any other text as a Json string, so a source that gives one Json value as text, such as the quoted string of google-mail-source, got it encoded a second time once its Kamelet declares json as its out data type. When the Content-Type header is application/json, with or without parameters, text that is exactly one Json value (a quoted string, a number, true, false or null) is Json already and is kept. Without that header the rule is unchanged, and text that is not Json, such as ORD-1, still becomes a Json string. The body types say so, and the catalog copy is regenerated. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../catalog/transformers/application-json.json | 2 +- .../apache/camel/transformer/application-json.json | 2 +- .../jackson/transform/JsonDataTypeTransformer.java | 40 +++++++++++++++++++--- .../transform/JsonDataTypeTransformerTest.java | 38 ++++++++++++++++++++ .../apache/camel/transformer/application-json.json | 2 +- .../transform/JsonDataTypeTransformer.java | 40 +++++++++++++++++++--- .../transform/JsonDataTypeTransformerTest.java | 38 ++++++++++++++++++++ 7 files changed, 151 insertions(+), 11 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-json.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-json.json index 35a29467a7fe..1cd3cd33feef 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-json.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/application-json.json @@ -21,7 +21,7 @@ "pojo", "null" ], - "description": "A Json object or array as text, bytes or a stream, a Map, a List, a JsonNode or a POJO; other text, also a Json number, boolean, null or quoted string, becomes a Json string; bytes and a stream are read as text with the exchange charset, so a payload that is not text must be encoded first, such as in Base64, or given a content class; with the X-Content-Class exchange property only a Json object or array as text or a body of that class; a null body becomes null" + "description": "A Json object or array as text, bytes or a stream, a Map, a List, a JsonNode or a POJO; other text, also a Json number, boolean, null or quoted string, becomes a Json string, unless the Content-Type header is application\/json, which keeps a Json value as it is; bytes and a stream are read as text with the exchange charset, so a payload that is not text must be encoded first, such as in Base64, or given a content class; with the X-Content-Class exchange property onl [...] }, "transformerOut": { "carriers": [ diff --git a/components/camel-jackson/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json b/components/camel-jackson/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json index 3e35142d8732..01a22812ae73 100644 --- a/components/camel-jackson/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json +++ b/components/camel-jackson/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json @@ -21,7 +21,7 @@ "pojo", "null" ], - "description": "A Json object or array as text, bytes or a stream, a Map, a List, a JsonNode or a POJO; other text, also a Json number, boolean, null or quoted string, becomes a Json string; bytes and a stream are read as text with the exchange charset, so a payload that is not text must be encoded first, such as in Base64, or given a content class; with the X-Content-Class exchange property only a Json object or array as text or a body of that class; a null body becomes null" + "description": "A Json object or array as text, bytes or a stream, a Map, a List, a JsonNode or a POJO; other text, also a Json number, boolean, null or quoted string, becomes a Json string, unless the Content-Type header is application\/json, which keeps a Json value as it is; bytes and a stream are read as text with the exchange charset, so a payload that is not text must be encoded first, such as in Base64, or given a content class; with the X-Content-Class exchange property onl [...] }, "transformerOut": { "carriers": [ diff --git a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformer.java b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformer.java index d451c1559983..5d0dee25e723 100644 --- a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformer.java +++ b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformer.java @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.Map; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import org.apache.camel.CamelExecutionException; import org.apache.camel.Exchange; @@ -36,6 +37,7 @@ import org.apache.camel.spi.DataType; import org.apache.camel.spi.DataTypeTransformer; import org.apache.camel.spi.MimeType; import org.apache.camel.spi.Transformer; +import org.apache.camel.util.StringHelper; /** * Data type uses Jackson data format to marshal given Exchange payload to a Json (binary byte array representation). @@ -48,7 +50,8 @@ import org.apache.camel.spi.Transformer; Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.MAP, Carrier.LIST, Carrier.POJO, Carrier.NULL }, description = "A Json object or array as text, bytes or a stream, a Map, a List, a" + " JsonNode or a POJO; other text, also a Json number, boolean, null or" - + " quoted string, becomes a Json string; bytes and a stream are read as" + + " quoted string, becomes a Json string, unless the Content-Type" + + " header is application/json, which keeps a Json value as it is; bytes and a stream are read as" + " text with the exchange charset, so a payload that is not text must be" + " encoded first, such as in Base64, or given a content class; with the" + " X-Content-Class exchange property only a Json object or array as text" @@ -68,10 +71,12 @@ public class JsonDataTypeTransformer extends Transformer { // a Map or a List has no class to write it as: it becomes a Json object or array marshalled = Json.mapper().writeValueAsBytes(body); } else if (!hasContentClass && isText(body)) { - // read as text with the exchange charset: a Json object or array is kept, and any other text, also a - // Json number, boolean, null or quoted string, becomes a Json string + // read as text with the exchange charset: a Json object or array is kept, and so is a Json number, + // boolean, null or quoted string when the Content-Type says the text is Json; any other text becomes a + // Json string String text = message.getMandatoryBody(String.class); - marshalled = Json.isJson(text) ? text.getBytes(StandardCharsets.UTF_8) : Json.mapper().writeValueAsBytes(text); + marshalled = Json.isJson(text) || isJsonContent(message) && isJsonValue(text) + ? text.getBytes(StandardCharsets.UTF_8) : Json.mapper().writeValueAsBytes(text); } else { String contentClass = SchemaHelper.resolveContentClass(message.getExchange(), JsonNode.class.getName()); Class<?> contentType @@ -92,6 +97,33 @@ public class JsonDataTypeTransformer extends Transformer { } } + /** + * Whether the Content-Type header says the body is Json (application/json), as a source that gives Json sets it. + */ + private static boolean isJsonContent(Message message) { + String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); + if (contentType == null) { + return false; + } + String mediaType = StringHelper.before(contentType, ";", contentType).trim(); + return MimeType.JSON.type().equalsIgnoreCase(mediaType); + } + + /** + * Whether the text is one Json value, such as a quoted string, a number, true, false or null. + */ + private static boolean isJsonValue(String text) { + if (text.isBlank()) { + return false; + } + try { + Json.mapper().readerFor(JsonNode.class).with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS).readValue(text); + return true; + } catch (JsonProcessingException e) { + return false; + } + } + /** * Text, bytes or a stream, which hold Json as text. */ diff --git a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformerTest.java b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformerTest.java index 0acae6cfd382..7fd2cb7f4e09 100644 --- a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformerTest.java +++ b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/transform/JsonDataTypeTransformerTest.java @@ -182,6 +182,44 @@ class JsonDataTypeTransformerTest { } } + @Test + void shouldKeepAJsonValueWhenTheContentTypeIsJson() throws Exception { + // CAMEL-27270: with the Content-Type application/json, text that is one Json value is Json already + for (String json : List.of("\"Hello\"", "42", "true", "null", " \"Hello\" ")) { + for (String contentType : List.of("application/json", "application/json; charset=UTF-8", "APPLICATION/JSON")) { + for (Object body : List.of(json, json.getBytes(StandardCharsets.UTF_8))) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, contentType); + exchange.getMessage().setBody(body); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + + Assertions.assertEquals(json, exchange.getMessage().getBody(String.class), json + " " + contentType); + Assertions.assertEquals("application/json", exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + } + } + // text that is not one Json value still becomes a Json string + Map<String, String> expected = new LinkedHashMap<>(); + expected.put("ORD-1", "\"ORD-1\""); + expected.put("42 abc", "\"42 abc\""); + expected.put("\"Hello\" \"World\"", "\"\\\"Hello\\\" \\\"World\\\"\""); + expected.put("", "\"\""); + for (Map.Entry<String, String> entry : expected.entrySet()) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "application/json"); + exchange.getMessage().setBody(entry.getKey()); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + + Assertions.assertEquals(entry.getValue(), exchange.getMessage().getBody(String.class), entry.getKey()); + } + // another Content-Type keeps the CAMEL-26300 rule + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "text/plain"); + exchange.getMessage().setBody("\"Hello\""); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + Assertions.assertEquals("\"\\\"Hello\\\"\"", exchange.getMessage().getBody(String.class)); + } + @Test void shouldReadBytesWithTheExchangeCharset() throws Exception { // CAMEL-26300: bytes are read as text with the charset of the exchange, and the Json is written in UTF-8 diff --git a/components/camel-jackson3/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json b/components/camel-jackson3/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json index 35a29467a7fe..1cd3cd33feef 100644 --- a/components/camel-jackson3/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json +++ b/components/camel-jackson3/src/generated/resources/META-INF/services/org/apache/camel/transformer/application-json.json @@ -21,7 +21,7 @@ "pojo", "null" ], - "description": "A Json object or array as text, bytes or a stream, a Map, a List, a JsonNode or a POJO; other text, also a Json number, boolean, null or quoted string, becomes a Json string; bytes and a stream are read as text with the exchange charset, so a payload that is not text must be encoded first, such as in Base64, or given a content class; with the X-Content-Class exchange property only a Json object or array as text or a body of that class; a null body becomes null" + "description": "A Json object or array as text, bytes or a stream, a Map, a List, a JsonNode or a POJO; other text, also a Json number, boolean, null or quoted string, becomes a Json string, unless the Content-Type header is application\/json, which keeps a Json value as it is; bytes and a stream are read as text with the exchange charset, so a payload that is not text must be encoded first, such as in Base64, or given a content class; with the X-Content-Class exchange property onl [...] }, "transformerOut": { "carriers": [ diff --git a/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformer.java b/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformer.java index 3518ce062ef1..f8161caf5203 100644 --- a/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformer.java +++ b/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformer.java @@ -34,7 +34,9 @@ import org.apache.camel.spi.DataType; import org.apache.camel.spi.DataTypeTransformer; import org.apache.camel.spi.MimeType; import org.apache.camel.spi.Transformer; +import org.apache.camel.util.StringHelper; import tools.jackson.core.JacksonException; +import tools.jackson.databind.DeserializationFeature; import tools.jackson.databind.JsonNode; /** @@ -48,7 +50,8 @@ import tools.jackson.databind.JsonNode; Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.MAP, Carrier.LIST, Carrier.POJO, Carrier.NULL }, description = "A Json object or array as text, bytes or a stream, a Map, a List, a" + " JsonNode or a POJO; other text, also a Json number, boolean, null or" - + " quoted string, becomes a Json string; bytes and a stream are read as" + + " quoted string, becomes a Json string, unless the Content-Type" + + " header is application/json, which keeps a Json value as it is; bytes and a stream are read as" + " text with the exchange charset, so a payload that is not text must be" + " encoded first, such as in Base64, or given a content class; with the" + " X-Content-Class exchange property only a Json object or array as text" @@ -68,10 +71,12 @@ public class JsonDataTypeTransformer extends Transformer { // a Map or a List has no class to write it as: it becomes a Json object or array marshalled = Json.mapper().writeValueAsBytes(body); } else if (!hasContentClass && isText(body)) { - // read as text with the exchange charset: a Json object or array is kept, and any other text, also a - // Json number, boolean, null or quoted string, becomes a Json string + // read as text with the exchange charset: a Json object or array is kept, and so is a Json number, + // boolean, null or quoted string when the Content-Type says the text is Json; any other text becomes a + // Json string String text = message.getMandatoryBody(String.class); - marshalled = Json.isJson(text) ? text.getBytes(StandardCharsets.UTF_8) : Json.mapper().writeValueAsBytes(text); + marshalled = Json.isJson(text) || isJsonContent(message) && isJsonValue(text) + ? text.getBytes(StandardCharsets.UTF_8) : Json.mapper().writeValueAsBytes(text); } else { String contentClass = SchemaHelper.resolveContentClass(message.getExchange(), JsonNode.class.getName()); Class<?> contentType @@ -92,6 +97,33 @@ public class JsonDataTypeTransformer extends Transformer { } } + /** + * Whether the Content-Type header says the body is Json (application/json), as a source that gives Json sets it. + */ + private static boolean isJsonContent(Message message) { + String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); + if (contentType == null) { + return false; + } + String mediaType = StringHelper.before(contentType, ";", contentType).trim(); + return MimeType.JSON.type().equalsIgnoreCase(mediaType); + } + + /** + * Whether the text is one Json value, such as a quoted string, a number, true, false or null. + */ + private static boolean isJsonValue(String text) { + if (text.isBlank()) { + return false; + } + try { + Json.mapper().readerFor(JsonNode.class).with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS).readValue(text); + return true; + } catch (JacksonException e) { + return false; + } + } + /** * Text, bytes or a stream, which hold Json as text. */ diff --git a/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformerTest.java b/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformerTest.java index 270627455b95..25df1c73d533 100644 --- a/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformerTest.java +++ b/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/transform/JsonDataTypeTransformerTest.java @@ -182,6 +182,44 @@ class JsonDataTypeTransformerTest { } } + @Test + void shouldKeepAJsonValueWhenTheContentTypeIsJson() throws Exception { + // CAMEL-27270: with the Content-Type application/json, text that is one Json value is Json already + for (String json : List.of("\"Hello\"", "42", "true", "null", " \"Hello\" ")) { + for (String contentType : List.of("application/json", "application/json; charset=UTF-8", "APPLICATION/JSON")) { + for (Object body : List.of(json, json.getBytes(StandardCharsets.UTF_8))) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, contentType); + exchange.getMessage().setBody(body); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + + Assertions.assertEquals(json, exchange.getMessage().getBody(String.class), json + " " + contentType); + Assertions.assertEquals("application/json", exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + } + } + // text that is not one Json value still becomes a Json string + Map<String, String> expected = new LinkedHashMap<>(); + expected.put("ORD-1", "\"ORD-1\""); + expected.put("42 abc", "\"42 abc\""); + expected.put("\"Hello\" \"World\"", "\"\\\"Hello\\\" \\\"World\\\"\""); + expected.put("", "\"\""); + for (Map.Entry<String, String> entry : expected.entrySet()) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "application/json"); + exchange.getMessage().setBody(entry.getKey()); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + + Assertions.assertEquals(entry.getValue(), exchange.getMessage().getBody(String.class), entry.getKey()); + } + // another Content-Type keeps the CAMEL-26300 rule + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "text/plain"); + exchange.getMessage().setBody("\"Hello\""); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + Assertions.assertEquals("\"\\\"Hello\\\"\"", exchange.getMessage().getBody(String.class)); + } + @Test void shouldReadBytesWithTheExchangeCharset() throws Exception { // CAMEL-26300: bytes are read as text with the charset of the exchange, and the Json is written in UTF-8
