davsclaus opened a new pull request, #30458:
URL: https://github.com/apache/camel/pull/30458

   Follow-up of #30453 (CAMEL-27258).
   
   ## Regression
   
   Since CAMEL-27258 the binary (`application-octet-stream`) data type keeps a 
stream cache body and only sets the Content-Type, where it used to read the 
body into a `byte[]`. The camel-kamelets Citrus tests `AvroIT` 
(`avro-data-type-test`) and `ProtobufIT` (`protobuf-data-type-test`) fail with 
Camel main:
   
   ```
   webhook-source (platform-http) -> data-type-action 
format=application-octet-stream
     -> resolve-pojo-schema-action mimeType=avro/binary schema=User
     -> data-type-action format=avro-x-struct -> log-action
   
   java.lang.NullPointerException: null value for (non-nullable) string at 
User.id
       at 
tools.jackson.databind.ObjectWriter.writeValueAsBytes(ObjectWriter.java:1112)
       at 
org.apache.camel.component.jackson3.avro.transform.AvroStructDataTypeTransformer.transform(AvroStructDataTypeTransformer.java:67)
   ```
   
   ## Cause
   
   Not a stream cache left unread or unreset: the message history shows the 53 
bytes of the request as an `InputStreamCache` all the way. 
`SchemaHelper.isPojo` takes every class outside `java.*`, `javax.*`, 
`com.sun.*` and `com.oracle.*` for a POJO, so it took the `InputStreamCache` 
for one. `resolve-pojo-schema-action` (`AvroSchemaResolver` / 
`ProtobufSchemaResolver`) then set `X-Content-Class` to 
`org.apache.camel.converter.stream.InputStreamCache`. `avro-x-struct` and 
`protobuf-x-struct` take the content-class branch: they write the body object 
with the schema (`writerFor(contentType).with(schema).writeValueAsBytes(body)`) 
instead of reading the Avro/Protobuf binary it holds, which fails on the first 
field. Before CAMEL-27258 the body was a `byte[]`, which `isPojo` does not take 
for a POJO.
   
   ## Fix
   
   `SchemaHelper.isPojo` returns false for a `StreamCache`, in `camel-jackson` 
and `camel-jackson3`, so `resolveContentClass` no longer reports the class of a 
stream cache body. This covers every user of 
`SchemaHelper.resolveContentClass`: the Json, Avro and Protobuf schema 
resolvers and their `*-x-struct`, `*-binary` and `*-x-java-object` data types. 
`ByteArrayDataTypeTransformer` is unchanged.
   
   ## Tests
   
   - `SchemaHelperTest` (camel-jackson, camel-jackson3): a stream cache is no 
POJO and gives no content class; a POJO still does.
   - `AvroStructDataTypeTransformerTest` (camel-jackson3-avro) and 
`ProtobufStructDataTypeTransformerTest` (camel-jackson3-protobuf): the Kamelet 
chain on an `InputStreamCache` of binary, i.e. `application-octet-stream`, then 
the schema resolver, then `avro-x-struct` / `protobuf-x-struct`, gives the 
`JsonNode` with the values. All six new tests fail without the fix.
   - `mvn test` in camel-jackson, camel-jackson3, camel-jackson-avro, 
camel-jackson3-avro, camel-jackson-protobuf and camel-jackson3-protobuf: all 
pass.
   - camel-kamelets `AvroIT` and `ProtobufIT`, with JBang resolving from a 
separate local Maven repository (a copy of `~/.m2` holding Camel main): without 
the fix both fail (`avro-data-type-test` with the NPE above, 
`protobuf-data-type-test` logs `Body: { }` as `protobuf-x-struct` writes the 
stream cache object as an empty message, and both time out on the expected log 
line); with the fix (only `camel-jackson3` rebuilt from this branch into that 
repository) both pass, 4 tests, 0 failures.
   
   ## Open question (not changed here)
   
   With stream caching off, a plain `InputStream` body is still read into a 
`byte[]` by `binary`; only a `StreamCache` is kept, as narrowed in the review 
of #30453, since a plain stream can be read only once. So `aws-s3-source` and 
the other sources with a `binary` default load whole objects into memory when 
stream caching is off. Should `binary` keep a plain `InputStream` too, or 
should we document stream caching with spooling 
(`camel.main.streamCachingSpoolEnabled=true`) for large objects?
   
   _Claude Code on behalf of Claus Ibsen_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   
   https://claude.ai/code/session_016Y4dDmBDsSWV117kccuNf9
   


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

To unsubscribe, e-mail: [email protected]

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

Reply via email to