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

   [CAMEL-25769](https://issues.apache.org/jira/browse/CAMEL-25769): with 
`bindingMode: json`, a GET sent with `Content-Type: text/plain` to an operation 
that answers an object was answered 500:
   
   ```
   curl -H 'Content-Type: text/plain;charset=UTF-8' 
http://localhost:8080/orders/ORD-1002
   500 Internal Server Error
   ... NoTypeConversionAvailableException - No type converter available to 
convert from type: java.util.LinkedHashMap to the required type: 
java.io.InputStream
   ```
   
   The same GET without a Content-Type, or with `application/json`, got the 
order as JSON.
   
   The cause was in `RestBindingAdvice.doMarshal`. The request's Content-Type 
was still on the message when the answer was made. `ensureHeaderContentType` 
keeps an existing Content-Type, and the answer is marshalled only when its 
Content-Type is json, so the Map was left as it was. The Content-Type of a 
request describes its body, and a GET has none. Most clients send none on a 
GET, but some do: the Citrus HTTP client sends `text/plain` by default 
(citrusframework/citrus#1826), which is how this was found, in the 
contracts/rest-code-first example (apache/camel-jbang-examples#118).
   
   ## What
   
   The binding remembers the request's Content-Type. It removes that 
Content-Type before the answer gets its own when all of these hold:
   
   - the route did not set another Content-Type;
   - the binding does not write that type (it is neither json under a json 
binding nor xml under an xml binding);
   - the answer is an object (a Map, a List, a POJO), not text, bytes or a 
stream.
   
   The binding then sets the Content-Type of its mode and marshals the answer, 
as for a request without a Content-Type.
   
   What stays as it was:
   
   - A Content-Type that the route sets itself still wins, so a route can 
answer `text/csv`.
   - A text answer to a `text/plain` request stays text.
   - A request type the binding writes (such as `application/json; 
charset=UTF-8`) is kept on the answer, as before.
   
   ## Tests
   
   - New `VertxPlatformHttpRestBindingRequestContentTypeTest` 
(camel-platform-http-vertx) covers the three cases:
     - An object answer is JSON for a GET with `text/plain`, with 
`application/json`, and without a Content-Type.
     - A text answer to a `text/plain` request stays text.
     - A Content-Type that the route sets wins.
     - Without the fix, the first test fails.
   - All tests of camel-platform-http-vertx pass (163), the REST tests of 
camel-core (55) and of camel-undertow (70).
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   
   https://claude.ai/code/session_01STT6whBgK1AqsSsUKrnE8m
   


-- 
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