This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27271 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 95c2f5e884e47a08067efbac2aed9c44ae299d79 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 20 07:53:24 2026 +0100 CAMEL-27271: camel-jackson, avro and protobuf - keep a more specific media type: application-json keeps a +json Content-Type, avro-binary and protobuf-binary keep bytes that are serialized already application-json always set Content-Type application/json, so the application/fhir+json or application/geo+json a Kamelet gives was lost. It now keeps an existing application/*+json Content-Type, and keeps a Json value given as text with it too (as with application/json, CAMEL-27270). avro-binary and protobuf-binary needed the schema, read bytes that are Avro or Protobuf already, wrote them again and set avro/binary or protobuf/binary, so the application/avro and application/protobuf of the serialize Kamelet actions were lost. A byte array or stream body whose Content-Type is application/avro or avro/binary (application/protobuf or protobuf/binary) is now kept as it is, with that Content-Type and without the schema. The body types and the catalog copies say so. Jackson 2 and 3. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../catalog/transformers/application-json.json | 5 +-- .../camel/catalog/transformers/avro-binary.json | 8 +++-- .../catalog/transformers/protobuf-binary.json | 8 +++-- .../org/apache/camel/transformer/avro-binary.json | 8 +++-- .../transform/AvroBinaryDataTypeTransformer.java | 32 ++++++++++++++++--- .../AvroBinaryDataTypeTransformerTest.java | 26 +++++++++++++++- .../apache/camel/transformer/protobuf-binary.json | 8 +++-- .../ProtobufBinaryDataTypeTransformer.java | 29 +++++++++++++++-- .../ProtobufBinaryDataTypeTransformerTest.java | 26 +++++++++++++++- .../apache/camel/transformer/application-json.json | 5 +-- .../jackson/transform/JsonDataTypeTransformer.java | 36 +++++++++++++++++----- .../transform/JsonDataTypeTransformerTest.java | 27 ++++++++++++++++ .../org/apache/camel/transformer/avro-binary.json | 8 +++-- .../transform/AvroBinaryDataTypeTransformer.java | 32 ++++++++++++++++--- .../AvroBinaryDataTypeTransformerTest.java | 26 +++++++++++++++- .../apache/camel/transformer/protobuf-binary.json | 8 +++-- .../ProtobufBinaryDataTypeTransformer.java | 29 +++++++++++++++-- .../ProtobufBinaryDataTypeTransformerTest.java | 26 +++++++++++++++- .../apache/camel/transformer/application-json.json | 5 +-- .../transform/JsonDataTypeTransformer.java | 36 +++++++++++++++++----- .../transform/JsonDataTypeTransformerTest.java | 27 ++++++++++++++++ 21 files changed, 361 insertions(+), 54 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 1cd3cd33feef..c4d62b71a395 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,14 +21,15 @@ "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, 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 [...] + "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 or a +json media type, 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 [...] }, "transformerOut": { "carriers": [ "bytes" ], "javaType": "byte[]", - "format": "json" + "format": "json", + "description": "Json as bytes, with the Content-Type application\/json, or the +json media type the message had, such as application\/fhir+json" } } } diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/avro-binary.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/avro-binary.json index 19cbcd77ed52..90b6086b85c4 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/avro-binary.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/avro-binary.json @@ -19,14 +19,16 @@ "file", "pojo" ], - "description": "Needs the Avro schema in the X-Content-Schema exchange property: a Json object or array as text, Avro binary, a JsonNode or a POJO; a Map or a null body fail" + "description": "Needs the Avro schema in the X-Content-Schema exchange property: a Json object or array as text, Avro binary, a JsonNode or a POJO; a Map or a null body fail. Bytes or a stream with the Content-Type application\/avro or avro\/binary are Avro binary already, and are kept as they are, without the schema" }, "transformerOut": { "carriers": [ - "bytes" + "bytes", + "stream" ], "javaType": "byte[]", - "format": "binary" + "format": "binary", + "description": "Avro binary as bytes; a body that is Avro binary already is kept, also a stream, with its Content-Type" } } } diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/protobuf-binary.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/protobuf-binary.json index 18ee8c76c93e..458d2fa8b874 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/protobuf-binary.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/transformers/protobuf-binary.json @@ -19,14 +19,16 @@ "file", "pojo" ], - "description": "Needs the Protobuf schema in the X-Content-Schema exchange property: a Json object or array as text, Protobuf binary, a JsonNode or a POJO; a Map or a null body fail" + "description": "Needs the Protobuf schema in the X-Content-Schema exchange property: a Json object or array as text, Protobuf binary, a JsonNode or a POJO; a Map or a null body fail. Bytes or a stream with the Content-Type application\/protobuf or protobuf\/binary are Protobuf binary already, and are kept as they are, without the schema" }, "transformerOut": { "carriers": [ - "bytes" + "bytes", + "stream" ], "javaType": "byte[]", - "format": "binary" + "format": "binary", + "description": "Protobuf binary as bytes; a body that is Protobuf binary already is kept, also a stream, with its Content-Type" } } } diff --git a/components/camel-jackson-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json b/components/camel-jackson-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json index eef5f00e9d5d..f53ab2817663 100644 --- a/components/camel-jackson-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json +++ b/components/camel-jackson-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json @@ -19,14 +19,16 @@ "file", "pojo" ], - "description": "Needs the Avro schema in the X-Content-Schema exchange property: a Json object or array as text, Avro binary, a JsonNode or a POJO; a Map or a null body fail" + "description": "Needs the Avro schema in the X-Content-Schema exchange property: a Json object or array as text, Avro binary, a JsonNode or a POJO; a Map or a null body fail. Bytes or a stream with the Content-Type application\/avro or avro\/binary are Avro binary already, and are kept as they are, without the schema" }, "transformerOut": { "carriers": [ - "bytes" + "bytes", + "stream" ], "javaType": "byte[]", - "format": "binary" + "format": "binary", + "description": "Avro binary as bytes; a body that is Avro binary already is kept, also a stream, with its Content-Type" } } } diff --git a/components/camel-jackson-avro/src/main/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformer.java b/components/camel-jackson-avro/src/main/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformer.java index 632434f5993c..1780c19e708d 100644 --- a/components/camel-jackson-avro/src/main/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformer.java +++ b/components/camel-jackson-avro/src/main/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformer.java @@ -36,6 +36,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 Avro data format to marshal given JsonNode in Exchange body to a binary (byte array) @@ -45,14 +46,23 @@ import org.apache.camel.spi.Transformer; @DataTypeTransformer(name = "avro-binary", description = "Transforms from JSon to binary (byte array) using Jackson Avro (supports Avro schema)") @BodyTypes(transformerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO }, - description = "Needs the Avro schema in the X-Content-Schema exchange property: a Json" - + " object or array as text, Avro binary, a JsonNode or a POJO; a Map or a" - + " null body fail"), - transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "binary")) + description = "Needs the Avro schema in the X-Content-Schema exchange property: a" + + " Json object or array as text, Avro binary, a JsonNode or a POJO; a" + + " Map or a null body fail. Bytes or a stream with the Content-Type" + + " application/avro or avro/binary are Avro binary already, and are kept as" + + " they are, without the schema"), + transformerOut = @BodyType(value = { Carrier.BYTES, Carrier.STREAM }, javaType = "byte[]", format = "binary", + description = "Avro binary as bytes; a body that is Avro binary already is kept," + + " also a stream, with its Content-Type")) public class AvroBinaryDataTypeTransformer extends Transformer { @Override public void transform(Message message, DataType fromType, DataType toType) { + if (isAvroBinary(message)) { + // Avro binary already, as a marshal to Avro leaves it: kept as it is, with its Content-Type + return; + } + AvroSchema schema = message.getExchange().getProperty(SchemaHelper.CONTENT_SCHEMA, AvroSchema.class); if (schema == null) { @@ -96,6 +106,20 @@ public class AvroBinaryDataTypeTransformer extends Transformer { .readValue(getBodyAsStream(message)); } + /** + * Whether the body is Avro binary already: bytes or a stream with the Content-Type {@code application/avro} or + * {@code avro/binary}, as a marshal to Avro and this data type set it. + */ + private static boolean isAvroBinary(Message message) { + Object body = message.getBody(); + String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); + if (!(body instanceof byte[] || body instanceof InputStream) || contentType == null) { + return false; + } + String mediaType = StringHelper.before(contentType, ";", contentType).trim(); + return MimeType.AVRO.type().equalsIgnoreCase(mediaType) || MimeType.AVRO_BINARY.type().equalsIgnoreCase(mediaType); + } + private InputStream getBodyAsStream(Message message) throws InvalidPayloadException { InputStream bodyStream = message.getBody(InputStream.class); diff --git a/components/camel-jackson-avro/src/test/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformerTest.java b/components/camel-jackson-avro/src/test/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformerTest.java index e3cd3d5c58e0..a0f86dfb6f5e 100644 --- a/components/camel-jackson-avro/src/test/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformerTest.java +++ b/components/camel-jackson-avro/src/test/java/org/apache/camel/component/jackson/avro/transform/AvroBinaryDataTypeTransformerTest.java @@ -118,6 +118,30 @@ class AvroBinaryDataTypeTransformerTest { Avro.mapper().reader().with(avroSchema).readTree(exchange.getMessage().getBody(byte[].class))), true); } + @Test + void shouldKeepAvroBinaryWithItsContentType() throws Exception { + // CAMEL-27271: bytes or a stream that the Content-Type says are Avro binary already are kept as they are, + // with that Content-Type, and need no schema + byte[] binary = new byte[] { 1, 2, 3 }; + for (String contentType : List.of("application/avro", "avro/binary", "APPLICATION/AVRO; charset=UTF-8")) { + for (Object body : List.of(binary, new ByteArrayInputStream(binary))) { + 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.assertSame(body, exchange.getMessage().getBody(), contentType); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + } + // other bytes still need the schema + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "application/octet-stream"); + exchange.getMessage().setBody(binary); + Assertions.assertThrows(CamelExecutionException.class, + () -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY)); + } + @Test public void shouldLookupDataTypeTransformer() throws Exception { Transformer transformer = camelContext.getTransformerRegistry() @@ -139,7 +163,7 @@ class AvroBinaryDataTypeTransformerTest { BodyTypes bodyTypes = AvroBinaryDataTypeTransformer.class.getAnnotation(BodyTypes.class); Assertions.assertEquals(List.of(Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO), List.of(bodyTypes.transformerIn().value())); - Assertions.assertEquals(List.of(Carrier.BYTES), List.of(bodyTypes.transformerOut().value())); + Assertions.assertEquals(List.of(Carrier.BYTES, Carrier.STREAM), List.of(bodyTypes.transformerOut().value())); Assertions.assertEquals(byte[].class.getCanonicalName(), bodyTypes.transformerOut().javaType()); Assertions.assertEquals("binary", bodyTypes.transformerOut().format()); String json = "{ \"name\": \"Goofy\", \"age\": 25 }"; diff --git a/components/camel-jackson-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json b/components/camel-jackson-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json index 7a0f6c858d5a..c3afd1e88768 100644 --- a/components/camel-jackson-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json +++ b/components/camel-jackson-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json @@ -19,14 +19,16 @@ "file", "pojo" ], - "description": "Needs the Protobuf schema in the X-Content-Schema exchange property: a Json object or array as text, Protobuf binary, a JsonNode or a POJO; a Map or a null body fail" + "description": "Needs the Protobuf schema in the X-Content-Schema exchange property: a Json object or array as text, Protobuf binary, a JsonNode or a POJO; a Map or a null body fail. Bytes or a stream with the Content-Type application\/protobuf or protobuf\/binary are Protobuf binary already, and are kept as they are, without the schema" }, "transformerOut": { "carriers": [ - "bytes" + "bytes", + "stream" ], "javaType": "byte[]", - "format": "binary" + "format": "binary", + "description": "Protobuf binary as bytes; a body that is Protobuf binary already is kept, also a stream, with its Content-Type" } } } diff --git a/components/camel-jackson-protobuf/src/main/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformer.java b/components/camel-jackson-protobuf/src/main/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformer.java index c94cb9993a72..fcf686b1e736 100644 --- a/components/camel-jackson-protobuf/src/main/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformer.java +++ b/components/camel-jackson-protobuf/src/main/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformer.java @@ -35,6 +35,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 Protobuf data format to marshal given JsonNode in Exchange body to a binary (byte array) @@ -46,12 +47,21 @@ import org.apache.camel.spi.Transformer; @BodyTypes(transformerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO }, description = "Needs the Protobuf schema in the X-Content-Schema exchange property: a" + " Json object or array as text, Protobuf binary, a JsonNode or a POJO; a" - + " Map or a null body fail"), - transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "binary")) + + " Map or a null body fail. Bytes or a stream with the Content-Type" + + " application/protobuf or protobuf/binary are Protobuf binary already, and are kept as" + + " they are, without the schema"), + transformerOut = @BodyType(value = { Carrier.BYTES, Carrier.STREAM }, javaType = "byte[]", format = "binary", + description = "Protobuf binary as bytes; a body that is Protobuf binary already is kept," + + " also a stream, with its Content-Type")) public class ProtobufBinaryDataTypeTransformer extends Transformer { @Override public void transform(Message message, DataType fromType, DataType toType) { + if (isProtobufBinary(message)) { + // Protobuf binary already, as a marshal to Protobuf leaves it: kept as it is, with its Content-Type + return; + } + ProtobufSchema schema = message.getExchange().getProperty(SchemaHelper.CONTENT_SCHEMA, ProtobufSchema.class); if (schema == null) { @@ -96,6 +106,21 @@ public class ProtobufBinaryDataTypeTransformer extends Transformer { .readValue(getBodyAsStream(message)); } + /** + * Whether the body is Protobuf binary already: bytes or a stream with the Content-Type {@code application/protobuf} + * or {@code protobuf/binary}, as a marshal to Protobuf and this data type set it. + */ + private static boolean isProtobufBinary(Message message) { + Object body = message.getBody(); + String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); + if (!(body instanceof byte[] || body instanceof InputStream) || contentType == null) { + return false; + } + String mediaType = StringHelper.before(contentType, ";", contentType).trim(); + return MimeType.PROTOBUF.type().equalsIgnoreCase(mediaType) + || MimeType.PROTOBUF_BINARY.type().equalsIgnoreCase(mediaType); + } + private InputStream getBodyAsStream(Message message) throws InvalidPayloadException { InputStream bodyStream = message.getBody(InputStream.class); diff --git a/components/camel-jackson-protobuf/src/test/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java b/components/camel-jackson-protobuf/src/test/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java index e0f973be04e8..3c609752fb2b 100644 --- a/components/camel-jackson-protobuf/src/test/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java +++ b/components/camel-jackson-protobuf/src/test/java/org/apache/camel/component/jackson/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java @@ -116,6 +116,30 @@ class ProtobufBinaryDataTypeTransformerTest { Protobuf.mapper().reader().with(protobufSchema).readTree(exchange.getMessage().getBody(byte[].class))), true); } + @Test + void shouldKeepProtobufBinaryWithItsContentType() throws Exception { + // CAMEL-27271: bytes or a stream that the Content-Type says are Protobuf binary already are kept as they are, + // with that Content-Type, and need no schema + byte[] binary = new byte[] { 1, 2, 3 }; + for (String contentType : List.of("application/protobuf", "protobuf/binary", "APPLICATION/PROTOBUF; charset=UTF-8")) { + for (Object body : List.of(binary, new ByteArrayInputStream(binary))) { + 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.assertSame(body, exchange.getMessage().getBody(), contentType); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + } + // other bytes still need the schema + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "application/octet-stream"); + exchange.getMessage().setBody(binary); + Assertions.assertThrows(CamelExecutionException.class, + () -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY)); + } + @Test public void shouldLookupDataTypeTransformer() throws Exception { Transformer transformer = camelContext.getTransformerRegistry() @@ -136,7 +160,7 @@ class ProtobufBinaryDataTypeTransformerTest { BodyTypes bodyTypes = ProtobufBinaryDataTypeTransformer.class.getAnnotation(BodyTypes.class); Assertions.assertEquals(List.of(Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO), List.of(bodyTypes.transformerIn().value())); - Assertions.assertEquals(List.of(Carrier.BYTES), List.of(bodyTypes.transformerOut().value())); + Assertions.assertEquals(List.of(Carrier.BYTES, Carrier.STREAM), List.of(bodyTypes.transformerOut().value())); Assertions.assertEquals(byte[].class.getCanonicalName(), bodyTypes.transformerOut().javaType()); Assertions.assertEquals("binary", bodyTypes.transformerOut().format()); String json = "{ \"name\": \"Goofy\", \"age\": 25 }"; 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 01a22812ae73..6efb6c46292e 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,14 +21,15 @@ "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, 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 [...] + "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 or a +json media type, 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 [...] }, "transformerOut": { "carriers": [ "bytes" ], "javaType": "byte[]", - "format": "json" + "format": "json", + "description": "Json as bytes, with the Content-Type application\/json, or the +json media type the message had, such as application\/fhir+json" } } } 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 5d0dee25e723..29d0af02530f 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 @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.Reader; import java.nio.charset.StandardCharsets; import java.util.Collection; +import java.util.Locale; import java.util.Map; import com.fasterxml.jackson.core.JsonProcessingException; @@ -51,12 +52,15 @@ import org.apache.camel.util.StringHelper; 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" + + " header is application/json or a +json media type, 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" + " or a body of that class; a null body becomes null"), - transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "json")) + transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "json", + description = "Json as bytes, with the Content-Type application/json, or the +json" + + " media type the message had, such as application/fhir+json")) public class JsonDataTypeTransformer extends Transformer { @Override @@ -86,7 +90,10 @@ public class JsonDataTypeTransformer extends Transformer { } message.setBody(marshalled); - message.setHeader(Exchange.CONTENT_TYPE, MimeType.JSON.type()); + if (!isJsonSuffixType(mediaType(message))) { + // a more specific Json media type, such as application/fhir+json, is kept + message.setHeader(Exchange.CONTENT_TYPE, MimeType.JSON.type()); + } String contentSchema = message.getExchange().getProperty(SchemaHelper.CONTENT_SCHEMA, String.class); if (contentSchema != null) { @@ -98,15 +105,30 @@ 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. + * Whether the Content-Type header says the body is Json, as a source that gives Json sets it: application/json, or + * a Json media type such as application/fhir+json. */ private static boolean isJsonContent(Message message) { + String mediaType = mediaType(message); + return MimeType.JSON.type().equals(mediaType) || isJsonSuffixType(mediaType); + } + + /** + * Whether the media type is a more specific Json media type, with the +json suffix, such as application/fhir+json. + */ + private static boolean isJsonSuffixType(String mediaType) { + return mediaType != null && mediaType.startsWith("application/") && mediaType.endsWith("+json"); + } + + /** + * The media type of the Content-Type header in lower case, without its parameters, or null when there is none. + */ + private static String mediaType(Message message) { String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); if (contentType == null) { - return false; + return null; } - String mediaType = StringHelper.before(contentType, ";", contentType).trim(); - return MimeType.JSON.type().equalsIgnoreCase(mediaType); + return StringHelper.before(contentType, ";", contentType).trim().toLowerCase(Locale.ROOT); } /** 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 7fd2cb7f4e09..2e58254cd5e8 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 @@ -220,6 +220,33 @@ class JsonDataTypeTransformerTest { Assertions.assertEquals("\"\\\"Hello\\\"\"", exchange.getMessage().getBody(String.class)); } + @Test + void shouldKeepAJsonSuffixContentType() throws Exception { + // CAMEL-27271: a more specific Json media type is kept, and a Json value given as text with it too + for (String contentType : List.of("application/fhir+json", "application/geo+json; charset=UTF-8")) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, contentType); + exchange.getMessage().setBody(Map.of("resourceType", "Patient")); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + + JSONAssert.assertEquals("{\"resourceType\":\"Patient\"}", exchange.getMessage().getBody(String.class), true); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + + exchange.getMessage().setBody("\"Hello\""); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + Assertions.assertEquals("\"Hello\"", exchange.getMessage().getBody(String.class)); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + // any other Content-Type becomes application/json + for (String contentType : List.of("text/plain", "application/xml", "application/json; charset=UTF-8")) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, contentType); + exchange.getMessage().setBody(Map.of("a", 1)); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + Assertions.assertEquals("application/json", exchange.getMessage().getHeader(Exchange.CONTENT_TYPE), contentType); + } + } + @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-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json b/components/camel-jackson3-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json index 19cbcd77ed52..90b6086b85c4 100644 --- a/components/camel-jackson3-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json +++ b/components/camel-jackson3-avro/src/generated/resources/META-INF/services/org/apache/camel/transformer/avro-binary.json @@ -19,14 +19,16 @@ "file", "pojo" ], - "description": "Needs the Avro schema in the X-Content-Schema exchange property: a Json object or array as text, Avro binary, a JsonNode or a POJO; a Map or a null body fail" + "description": "Needs the Avro schema in the X-Content-Schema exchange property: a Json object or array as text, Avro binary, a JsonNode or a POJO; a Map or a null body fail. Bytes or a stream with the Content-Type application\/avro or avro\/binary are Avro binary already, and are kept as they are, without the schema" }, "transformerOut": { "carriers": [ - "bytes" + "bytes", + "stream" ], "javaType": "byte[]", - "format": "binary" + "format": "binary", + "description": "Avro binary as bytes; a body that is Avro binary already is kept, also a stream, with its Content-Type" } } } diff --git a/components/camel-jackson3-avro/src/main/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformer.java b/components/camel-jackson3-avro/src/main/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformer.java index 3c1875a1c814..73d0f35856e8 100644 --- a/components/camel-jackson3-avro/src/main/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformer.java +++ b/components/camel-jackson3-avro/src/main/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformer.java @@ -34,6 +34,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; import tools.jackson.core.JacksonException; import tools.jackson.databind.JsonNode; import tools.jackson.dataformat.avro.AvroSchema; @@ -46,14 +47,23 @@ import tools.jackson.dataformat.avro.AvroSchema; @DataTypeTransformer(name = "avro-binary", description = "Transforms from JSon to binary (byte array) using Jackson Avro (supports Avro schema)") @BodyTypes(transformerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO }, - description = "Needs the Avro schema in the X-Content-Schema exchange property: a Json" - + " object or array as text, Avro binary, a JsonNode or a POJO; a Map or a" - + " null body fail"), - transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "binary")) + description = "Needs the Avro schema in the X-Content-Schema exchange property: a" + + " Json object or array as text, Avro binary, a JsonNode or a POJO; a" + + " Map or a null body fail. Bytes or a stream with the Content-Type" + + " application/avro or avro/binary are Avro binary already, and are kept as" + + " they are, without the schema"), + transformerOut = @BodyType(value = { Carrier.BYTES, Carrier.STREAM }, javaType = "byte[]", format = "binary", + description = "Avro binary as bytes; a body that is Avro binary already is kept," + + " also a stream, with its Content-Type")) public class AvroBinaryDataTypeTransformer extends Transformer { @Override public void transform(Message message, DataType fromType, DataType toType) { + if (isAvroBinary(message)) { + // Avro binary already, as a marshal to Avro leaves it: kept as it is, with its Content-Type + return; + } + AvroSchema schema = message.getExchange().getProperty(SchemaHelper.CONTENT_SCHEMA, AvroSchema.class); if (schema == null) { @@ -97,6 +107,20 @@ public class AvroBinaryDataTypeTransformer extends Transformer { .readValue(getBodyAsStream(message)); } + /** + * Whether the body is Avro binary already: bytes or a stream with the Content-Type {@code application/avro} or + * {@code avro/binary}, as a marshal to Avro and this data type set it. + */ + private static boolean isAvroBinary(Message message) { + Object body = message.getBody(); + String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); + if (!(body instanceof byte[] || body instanceof InputStream) || contentType == null) { + return false; + } + String mediaType = StringHelper.before(contentType, ";", contentType).trim(); + return MimeType.AVRO.type().equalsIgnoreCase(mediaType) || MimeType.AVRO_BINARY.type().equalsIgnoreCase(mediaType); + } + private InputStream getBodyAsStream(Message message) throws InvalidPayloadException { InputStream bodyStream = message.getBody(InputStream.class); diff --git a/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformerTest.java b/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformerTest.java index c4ce9568fdf5..68d9b70a7786 100644 --- a/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformerTest.java +++ b/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroBinaryDataTypeTransformerTest.java @@ -130,6 +130,30 @@ class AvroBinaryDataTypeTransformerTest { () -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY)); } + @Test + void shouldKeepAvroBinaryWithItsContentType() throws Exception { + // CAMEL-27271: bytes or a stream that the Content-Type says are Avro binary already are kept as they are, + // with that Content-Type, and need no schema + byte[] binary = new byte[] { 1, 2, 3 }; + for (String contentType : List.of("application/avro", "avro/binary", "APPLICATION/AVRO; charset=UTF-8")) { + for (Object body : List.of(binary, new ByteArrayInputStream(binary))) { + 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.assertSame(body, exchange.getMessage().getBody(), contentType); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + } + // other bytes still need the schema + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "application/octet-stream"); + exchange.getMessage().setBody(binary); + Assertions.assertThrows(CamelExecutionException.class, + () -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY)); + } + @Test public void shouldLookupDataTypeTransformer() throws Exception { Transformer transformer = camelContext.getTransformerRegistry() @@ -151,7 +175,7 @@ class AvroBinaryDataTypeTransformerTest { BodyTypes bodyTypes = AvroBinaryDataTypeTransformer.class.getAnnotation(BodyTypes.class); Assertions.assertEquals(List.of(Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO), List.of(bodyTypes.transformerIn().value())); - Assertions.assertEquals(List.of(Carrier.BYTES), List.of(bodyTypes.transformerOut().value())); + Assertions.assertEquals(List.of(Carrier.BYTES, Carrier.STREAM), List.of(bodyTypes.transformerOut().value())); Assertions.assertEquals(byte[].class.getCanonicalName(), bodyTypes.transformerOut().javaType()); Assertions.assertEquals("binary", bodyTypes.transformerOut().format()); String json = "{ \"name\": \"Goofy\", \"age\": 25 }"; diff --git a/components/camel-jackson3-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json b/components/camel-jackson3-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json index 18ee8c76c93e..458d2fa8b874 100644 --- a/components/camel-jackson3-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json +++ b/components/camel-jackson3-protobuf/src/generated/resources/META-INF/services/org/apache/camel/transformer/protobuf-binary.json @@ -19,14 +19,16 @@ "file", "pojo" ], - "description": "Needs the Protobuf schema in the X-Content-Schema exchange property: a Json object or array as text, Protobuf binary, a JsonNode or a POJO; a Map or a null body fail" + "description": "Needs the Protobuf schema in the X-Content-Schema exchange property: a Json object or array as text, Protobuf binary, a JsonNode or a POJO; a Map or a null body fail. Bytes or a stream with the Content-Type application\/protobuf or protobuf\/binary are Protobuf binary already, and are kept as they are, without the schema" }, "transformerOut": { "carriers": [ - "bytes" + "bytes", + "stream" ], "javaType": "byte[]", - "format": "binary" + "format": "binary", + "description": "Protobuf binary as bytes; a body that is Protobuf binary already is kept, also a stream, with its Content-Type" } } } diff --git a/components/camel-jackson3-protobuf/src/main/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformer.java b/components/camel-jackson3-protobuf/src/main/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformer.java index 400deeba15c1..dd23d9ea4ec4 100644 --- a/components/camel-jackson3-protobuf/src/main/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformer.java +++ b/components/camel-jackson3-protobuf/src/main/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformer.java @@ -33,6 +33,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; import tools.jackson.core.JacksonException; import tools.jackson.databind.JsonNode; import tools.jackson.dataformat.protobuf.schema.ProtobufSchema; @@ -47,12 +48,21 @@ import tools.jackson.dataformat.protobuf.schema.ProtobufSchema; @BodyTypes(transformerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO }, description = "Needs the Protobuf schema in the X-Content-Schema exchange property: a" + " Json object or array as text, Protobuf binary, a JsonNode or a POJO; a" - + " Map or a null body fail"), - transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "binary")) + + " Map or a null body fail. Bytes or a stream with the Content-Type" + + " application/protobuf or protobuf/binary are Protobuf binary already, and are kept as" + + " they are, without the schema"), + transformerOut = @BodyType(value = { Carrier.BYTES, Carrier.STREAM }, javaType = "byte[]", format = "binary", + description = "Protobuf binary as bytes; a body that is Protobuf binary already is kept," + + " also a stream, with its Content-Type")) public class ProtobufBinaryDataTypeTransformer extends Transformer { @Override public void transform(Message message, DataType fromType, DataType toType) { + if (isProtobufBinary(message)) { + // Protobuf binary already, as a marshal to Protobuf leaves it: kept as it is, with its Content-Type + return; + } + ProtobufSchema schema = message.getExchange().getProperty(SchemaHelper.CONTENT_SCHEMA, ProtobufSchema.class); if (schema == null) { @@ -97,6 +107,21 @@ public class ProtobufBinaryDataTypeTransformer extends Transformer { .readValue(getBodyAsStream(message)); } + /** + * Whether the body is Protobuf binary already: bytes or a stream with the Content-Type {@code application/protobuf} + * or {@code protobuf/binary}, as a marshal to Protobuf and this data type set it. + */ + private static boolean isProtobufBinary(Message message) { + Object body = message.getBody(); + String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); + if (!(body instanceof byte[] || body instanceof InputStream) || contentType == null) { + return false; + } + String mediaType = StringHelper.before(contentType, ";", contentType).trim(); + return MimeType.PROTOBUF.type().equalsIgnoreCase(mediaType) + || MimeType.PROTOBUF_BINARY.type().equalsIgnoreCase(mediaType); + } + private InputStream getBodyAsStream(Message message) throws InvalidPayloadException { InputStream bodyStream = message.getBody(InputStream.class); diff --git a/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java b/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java index 56f87be5718c..471c796db70c 100644 --- a/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java +++ b/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufBinaryDataTypeTransformerTest.java @@ -128,6 +128,30 @@ class ProtobufBinaryDataTypeTransformerTest { () -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY)); } + @Test + void shouldKeepProtobufBinaryWithItsContentType() throws Exception { + // CAMEL-27271: bytes or a stream that the Content-Type says are Protobuf binary already are kept as they are, + // with that Content-Type, and need no schema + byte[] binary = new byte[] { 1, 2, 3 }; + for (String contentType : List.of("application/protobuf", "protobuf/binary", "APPLICATION/PROTOBUF; charset=UTF-8")) { + for (Object body : List.of(binary, new ByteArrayInputStream(binary))) { + 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.assertSame(body, exchange.getMessage().getBody(), contentType); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + } + // other bytes still need the schema + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, "application/octet-stream"); + exchange.getMessage().setBody(binary); + Assertions.assertThrows(CamelExecutionException.class, + () -> transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY)); + } + @Test public void shouldLookupDataTypeTransformer() throws Exception { Transformer transformer = camelContext.getTransformerRegistry() @@ -148,7 +172,7 @@ class ProtobufBinaryDataTypeTransformerTest { BodyTypes bodyTypes = ProtobufBinaryDataTypeTransformer.class.getAnnotation(BodyTypes.class); Assertions.assertEquals(List.of(Carrier.TEXT, Carrier.BYTES, Carrier.STREAM, Carrier.FILE, Carrier.POJO), List.of(bodyTypes.transformerIn().value())); - Assertions.assertEquals(List.of(Carrier.BYTES), List.of(bodyTypes.transformerOut().value())); + Assertions.assertEquals(List.of(Carrier.BYTES, Carrier.STREAM), List.of(bodyTypes.transformerOut().value())); Assertions.assertEquals(byte[].class.getCanonicalName(), bodyTypes.transformerOut().javaType()); Assertions.assertEquals("binary", bodyTypes.transformerOut().format()); String json = "{ \"name\": \"Goofy\", \"age\": 25 }"; 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 1cd3cd33feef..c4d62b71a395 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,14 +21,15 @@ "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, 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 [...] + "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 or a +json media type, 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 [...] }, "transformerOut": { "carriers": [ "bytes" ], "javaType": "byte[]", - "format": "json" + "format": "json", + "description": "Json as bytes, with the Content-Type application\/json, or the +json media type the message had, such as application\/fhir+json" } } } 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 f8161caf5203..7fae360818ba 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 @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.Reader; import java.nio.charset.StandardCharsets; import java.util.Collection; +import java.util.Locale; import java.util.Map; import org.apache.camel.CamelExecutionException; @@ -51,12 +52,15 @@ import tools.jackson.databind.JsonNode; 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" + + " header is application/json or a +json media type, 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" + " or a body of that class; a null body becomes null"), - transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "json")) + transformerOut = @BodyType(value = Carrier.BYTES, javaType = "byte[]", format = "json", + description = "Json as bytes, with the Content-Type application/json, or the +json" + + " media type the message had, such as application/fhir+json")) public class JsonDataTypeTransformer extends Transformer { @Override @@ -86,7 +90,10 @@ public class JsonDataTypeTransformer extends Transformer { } message.setBody(marshalled); - message.setHeader(Exchange.CONTENT_TYPE, MimeType.JSON.type()); + if (!isJsonSuffixType(mediaType(message))) { + // a more specific Json media type, such as application/fhir+json, is kept + message.setHeader(Exchange.CONTENT_TYPE, MimeType.JSON.type()); + } String contentSchema = message.getExchange().getProperty(SchemaHelper.CONTENT_SCHEMA, String.class); if (contentSchema != null) { @@ -98,15 +105,30 @@ 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. + * Whether the Content-Type header says the body is Json, as a source that gives Json sets it: application/json, or + * a Json media type such as application/fhir+json. */ private static boolean isJsonContent(Message message) { + String mediaType = mediaType(message); + return MimeType.JSON.type().equals(mediaType) || isJsonSuffixType(mediaType); + } + + /** + * Whether the media type is a more specific Json media type, with the +json suffix, such as application/fhir+json. + */ + private static boolean isJsonSuffixType(String mediaType) { + return mediaType != null && mediaType.startsWith("application/") && mediaType.endsWith("+json"); + } + + /** + * The media type of the Content-Type header in lower case, without its parameters, or null when there is none. + */ + private static String mediaType(Message message) { String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class); if (contentType == null) { - return false; + return null; } - String mediaType = StringHelper.before(contentType, ";", contentType).trim(); - return MimeType.JSON.type().equalsIgnoreCase(mediaType); + return StringHelper.before(contentType, ";", contentType).trim().toLowerCase(Locale.ROOT); } /** 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 25df1c73d533..db79caf511b4 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 @@ -220,6 +220,33 @@ class JsonDataTypeTransformerTest { Assertions.assertEquals("\"\\\"Hello\\\"\"", exchange.getMessage().getBody(String.class)); } + @Test + void shouldKeepAJsonSuffixContentType() throws Exception { + // CAMEL-27271: a more specific Json media type is kept, and a Json value given as text with it too + for (String contentType : List.of("application/fhir+json", "application/geo+json; charset=UTF-8")) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, contentType); + exchange.getMessage().setBody(Map.of("resourceType", "Patient")); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + + JSONAssert.assertEquals("{\"resourceType\":\"Patient\"}", exchange.getMessage().getBody(String.class), true); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + + exchange.getMessage().setBody("\"Hello\""); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + Assertions.assertEquals("\"Hello\"", exchange.getMessage().getBody(String.class)); + Assertions.assertEquals(contentType, exchange.getMessage().getHeader(Exchange.CONTENT_TYPE)); + } + // any other Content-Type becomes application/json + for (String contentType : List.of("text/plain", "application/xml", "application/json; charset=UTF-8")) { + Exchange exchange = new DefaultExchange(camelContext); + exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, contentType); + exchange.getMessage().setBody(Map.of("a", 1)); + transformer.transform(exchange.getMessage(), DataType.ANY, DataType.ANY); + Assertions.assertEquals("application/json", exchange.getMessage().getHeader(Exchange.CONTENT_TYPE), contentType); + } + } + @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
