This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new a04c45d07df4 CAMEL-27258: camel-jackson - a stream cache body is no
POJO for the content class of a schema (follow-up) (#30458)
a04c45d07df4 is described below
commit a04c45d07df42fc68035cfa06c33007d7665cdee
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 20 08:18:07 2026 +0100
CAMEL-27258: camel-jackson - a stream cache body is no POJO for the content
class of a schema (follow-up) (#30458)
Since CAMEL-27258 the binary (application-octet-stream) data type keeps a
stream cache body instead of reading it into a byte[]. SchemaHelper.isPojo
took the stream cache, such as the InputStreamCache of an HTTP request, for
a POJO, so the Avro, Protobuf and Json schema resolvers set its class as the
content class. The avro-x-struct and protobuf-x-struct data types then wrote
the stream cache object with the schema instead of reading the binary it
holds, and failed with "null value for (non-nullable) string".
A stream cache holds raw content and is no longer a POJO, in camel-jackson
and camel-jackson3.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/jackson/SchemaHelper.java | 6 +++
.../camel/component/jackson/SchemaHelperTest.java | 54 ++++++++++++++++++++++
.../AvroStructDataTypeTransformerTest.java | 25 ++++++++++
.../ProtobufStructDataTypeTransformerTest.java | 27 +++++++++++
.../camel/component/jackson3/SchemaHelper.java | 6 +++
.../camel/component/jackson3/SchemaHelperTest.java | 54 ++++++++++++++++++++++
6 files changed, 172 insertions(+)
diff --git
a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/SchemaHelper.java
b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/SchemaHelper.java
index 866ec5a3d0f7..fa632935ffdc 100644
---
a/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/SchemaHelper.java
+++
b/components/camel-jackson/src/main/java/org/apache/camel/component/jackson/SchemaHelper.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.jackson;
import org.apache.camel.Exchange;
+import org.apache.camel.StreamCache;
public class SchemaHelper {
@@ -74,6 +75,11 @@ public class SchemaHelper {
}
public static boolean isPojo(Class<?> type) {
+ if (StreamCache.class.isAssignableFrom(type)) {
+ // a stream cache holds the raw content, such as the body of an
HTTP request, and is no POJO
+ return false;
+ }
+
Package pkg = type.getPackage();
if (pkg != null) {
if (pkg.getName().startsWith("java")
diff --git
a/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/SchemaHelperTest.java
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/SchemaHelperTest.java
new file mode 100644
index 000000000000..31b8ead6ad2c
--- /dev/null
+++
b/components/camel-jackson/src/test/java/org/apache/camel/component/jackson/SchemaHelperTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jackson;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.converter.stream.InputStreamCache;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class SchemaHelperTest {
+
+ @Test
+ void streamCacheIsNoPojo() {
+ // CAMEL-27258: the binary data type keeps a stream cache body, which
holds raw content and is no POJO
+ assertFalse(SchemaHelper.isPojo(InputStreamCache.class));
+ assertFalse(SchemaHelper.isPojo(byte[].class));
+ assertFalse(SchemaHelper.isPojo(String.class));
+ assertTrue(SchemaHelper.isPojo(TestPojo.class));
+ }
+
+ @Test
+ void streamCacheBodyHasNoContentClass() {
+ Exchange exchange = new DefaultExchange(new DefaultCamelContext());
+ exchange.getMessage().setBody(new InputStreamCache("{ \"name\":
\"Goofy\" }".getBytes(StandardCharsets.UTF_8)));
+
+ assertNull(SchemaHelper.resolveContentClass(exchange, null));
+ assertEquals("fallback", SchemaHelper.resolveContentClass(exchange,
"fallback"));
+
+ exchange.getMessage().setBody(new TestPojo());
+ assertEquals(TestPojo.class.getName(),
SchemaHelper.resolveContentClass(exchange, null));
+ }
+}
diff --git
a/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroStructDataTypeTransformerTest.java
b/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroStructDataTypeTransformerTest.java
index 102fc907d5d1..69e0b8d9bafb 100644
---
a/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroStructDataTypeTransformerTest.java
+++
b/components/camel-jackson3-avro/src/test/java/org/apache/camel/component/jackson3/avro/transform/AvroStructDataTypeTransformerTest.java
@@ -32,6 +32,7 @@ import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
import org.apache.camel.component.jackson3.SchemaHelper;
import org.apache.camel.component.jackson3.transform.Json;
+import org.apache.camel.converter.stream.InputStreamCache;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.BodyType.Carrier;
import org.apache.camel.spi.BodyTypes;
@@ -151,6 +152,30 @@ class AvroStructDataTypeTransformerTest {
Assertions.assertEquals(ObjectNode.class,
exchange.getMessage().getBody().getClass());
}
+ @Test
+ void shouldHandleAvroBinaryStreamCacheAfterSchemaResolver() throws
Exception {
+ // CAMEL-27258: the binary data type keeps a stream cache body, such
as the body of an HTTP request; the
+ // schema resolver must not take the stream cache for a POJO and set
its class as the content class
+ Exchange exchange = new DefaultExchange(camelContext);
+ byte[] binary =
Avro.mapper().writer(getSchema()).writeValueAsBytes(new Person("Sheldon", 28));
+ exchange.getMessage().setBody(new InputStreamCache(binary));
+
+ camelContext.getTransformerRegistry().resolveTransformer(new
TransformerKey("application-octet-stream"))
+ .transform(exchange.getMessage(), DataType.ANY, DataType.ANY);
+ Assertions.assertInstanceOf(InputStreamCache.class,
exchange.getMessage().getBody());
+
+ AvroSchemaResolver schemaResolver = new AvroSchemaResolver();
+ schemaResolver.setSchema(getSchema().getAvroSchema().toString());
+ schemaResolver.process(exchange);
+
Assertions.assertNull(exchange.getProperty(SchemaHelper.CONTENT_CLASS));
+
+ transformer.transform(exchange.getMessage(), DataType.ANY,
DataType.ANY);
+
+ JsonNode body = exchange.getMessage().getBody(JsonNode.class);
+ Assertions.assertEquals("Sheldon", body.get("name").asString());
+ Assertions.assertEquals(28, body.get("age").asInt());
+ }
+
@Test
public void shouldLookupDataTypeTransformer() throws Exception {
Transformer transformer = camelContext.getTransformerRegistry()
diff --git
a/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufStructDataTypeTransformerTest.java
b/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufStructDataTypeTransformerTest.java
index 67c10feecae9..837d6a64f798 100644
---
a/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufStructDataTypeTransformerTest.java
+++
b/components/camel-jackson3-protobuf/src/test/java/org/apache/camel/component/jackson3/protobuf/transform/ProtobufStructDataTypeTransformerTest.java
@@ -30,6 +30,7 @@ import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
import org.apache.camel.component.jackson3.SchemaHelper;
import org.apache.camel.component.jackson3.transform.Json;
+import org.apache.camel.converter.stream.InputStreamCache;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.BodyType.Carrier;
import org.apache.camel.spi.BodyTypes;
@@ -150,6 +151,32 @@ class ProtobufStructDataTypeTransformerTest {
Assertions.assertEquals(ProtobufStructDataTypeTransformer.class,
transformer.getClass());
}
+ @Test
+ void shouldHandleProtobufBinaryStreamCacheAfterSchemaResolver() throws
Exception {
+ // CAMEL-27258: the binary data type keeps a stream cache body, such
as the body of an HTTP request; the
+ // schema resolver must not take the stream cache for a POJO and set
its class as the content class
+ Exchange exchange = new DefaultExchange(camelContext);
+ byte[] binary =
Protobuf.mapper().writer(getSchema()).writeValueAsBytes(new Person("Sheldon",
28));
+ exchange.getMessage().setBody(new InputStreamCache(binary));
+
+ camelContext.getTransformerRegistry().resolveTransformer(new
TransformerKey("application-octet-stream"))
+ .transform(exchange.getMessage(), DataType.ANY, DataType.ANY);
+ Assertions.assertInstanceOf(InputStreamCache.class,
exchange.getMessage().getBody());
+
+ ProtobufSchemaResolver schemaResolver = new ProtobufSchemaResolver();
+ schemaResolver.setSchema(new String(
+
ProtobufStructDataTypeTransformer.class.getResourceAsStream("Person.proto").readAllBytes(),
+ StandardCharsets.UTF_8));
+ schemaResolver.process(exchange);
+
Assertions.assertNull(exchange.getProperty(SchemaHelper.CONTENT_CLASS));
+
+ transformer.transform(exchange.getMessage(), DataType.ANY,
DataType.ANY);
+
+ JsonNode body = exchange.getMessage().getBody(JsonNode.class);
+ Assertions.assertEquals("Sheldon", body.get("name").asString());
+ Assertions.assertEquals(28, body.get("age").asInt());
+ }
+
private ProtobufSchema getSchema() throws IOException {
return Protobuf.mapper().schemaLoader()
.load(ProtobufStructDataTypeTransformer.class.getResourceAsStream("Person.proto"));
diff --git
a/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/SchemaHelper.java
b/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/SchemaHelper.java
index 1cad538cc255..abd5b0faf290 100644
---
a/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/SchemaHelper.java
+++
b/components/camel-jackson3/src/main/java/org/apache/camel/component/jackson3/SchemaHelper.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.jackson3;
import org.apache.camel.Exchange;
+import org.apache.camel.StreamCache;
public class SchemaHelper {
@@ -74,6 +75,11 @@ public class SchemaHelper {
}
public static boolean isPojo(Class<?> type) {
+ if (StreamCache.class.isAssignableFrom(type)) {
+ // a stream cache holds the raw content, such as the body of an
HTTP request, and is no POJO
+ return false;
+ }
+
Package pkg = type.getPackage();
if (pkg != null) {
if (pkg.getName().startsWith("java")
diff --git
a/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/SchemaHelperTest.java
b/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/SchemaHelperTest.java
new file mode 100644
index 000000000000..75b2dec8095f
--- /dev/null
+++
b/components/camel-jackson3/src/test/java/org/apache/camel/component/jackson3/SchemaHelperTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jackson3;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.converter.stream.InputStreamCache;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class SchemaHelperTest {
+
+ @Test
+ void streamCacheIsNoPojo() {
+ // CAMEL-27258: the binary data type keeps a stream cache body, which
holds raw content and is no POJO
+ assertFalse(SchemaHelper.isPojo(InputStreamCache.class));
+ assertFalse(SchemaHelper.isPojo(byte[].class));
+ assertFalse(SchemaHelper.isPojo(String.class));
+ assertTrue(SchemaHelper.isPojo(TestPojo.class));
+ }
+
+ @Test
+ void streamCacheBodyHasNoContentClass() {
+ Exchange exchange = new DefaultExchange(new DefaultCamelContext());
+ exchange.getMessage().setBody(new InputStreamCache("{ \"name\":
\"Goofy\" }".getBytes(StandardCharsets.UTF_8)));
+
+ assertNull(SchemaHelper.resolveContentClass(exchange, null));
+ assertEquals("fallback", SchemaHelper.resolveContentClass(exchange,
"fallback"));
+
+ exchange.getMessage().setBody(new TestPojo());
+ assertEquals(TestPojo.class.getName(),
SchemaHelper.resolveContentClass(exchange, null));
+ }
+}