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

   Fixes [CAMEL-26266](https://issues.apache.org/jira/browse/CAMEL-26266), 
found while adding the catalog body types (CAMEL-24961).
   
   ## 1. A result without a body field was lost
   
   **Was:** `invokeFunction` set the body to the top-level `body` field of the 
result only (`FunctionGraphUtils.extractJsonFieldAsString`). A function that 
returned a JSON object without a `body` field left the body null, so its result 
was lost. A result that was not a JSON object (a JSON array, a string, a number 
or plain text) failed the exchange with a Gson `JsonSyntaxException` ("Expected 
a com.google.gson.JsonObject but was ...").
   
   **Now:** the new `FunctionGraphUtils.extractResultBody` keeps the existing 
rule for a JSON object with a top-level `body` field, such as the result of a 
function behind an API Gateway trigger: the body is that field (a string as it 
is, an object or an array as JSON, null when the field is null). Any other 
result is given whole, as FunctionGraph returned it. No result, an empty result 
or a JSON `null` still gives no body. No new option: the whole result is only 
given where the body was null or the call failed before. 
`extractJsonFieldAsString` is unchanged (it shares the field formatting with 
the new method).
   
   ## 2. A JSON event as bytes or a stream, or a JSON array, failed
   
   **Was:** the producer took a `Map`, or a `String` with a JSON object. Bytes, 
a stream or a file failed with "Exchange body is mandatory and should be a 
valid Map or JSON string", and a JSON array as a String failed with "Request 
body must be a JSON or a HashMap".
   
   **Now:** a `Map` is sent as it is, as before. Another body is converted to a 
String with the type converter first (`getMandatoryBody(String.class)`), so a 
JSON object as a String, byte[], InputStream, File or GenericFile works, read 
with the charset of the exchange (UTF-8 by default). It is parsed with the 
Jackson the module already uses, into an ordered map.
   
   FunctionGraph takes a JSON object as the event: the invoke API documents the 
request body as an object with user-defined keys, and the 
`InvokeFunctionRequest` of the SDK takes a `Map<String, Object>` (required, 
non-null). So a JSON array or another JSON value that is not an object still 
fails, now with an `IllegalArgumentException` that says what it is and what to 
do, for example "... but the body is a JSON array: wrap it in a JSON object". 
Text that is not JSON, such as the `toString()` of a POJO, fails with "... the 
body (<class>) is not valid JSON", with the Jackson error as the cause. No body 
still fails with an `IllegalArgumentException`. The function is not called in 
these cases.
   
   ## Tests
   
   - New `InvokeFunctionBodyTest` (17 tests) with a recording 
`FunctionGraphClient` set on the endpoint. It covers the event as String, 
byte[] (with a non-ASCII character), InputStream, File, GenericFile and Map, a 
JSON array, a JSON number as bytes, invalid JSON, a POJO and no body; and a 
result without a `body` field, a JSON array result, a plain-text result, an API 
Gateway style result with a `body` field, a null `body` field and no result.
   - With the `src/main` changes reverted, 11 of the 17 new tests fail: the 4 
byte[]/stream/File/GenericFile events with the old "Exchange body is mandatory" 
exception, the array and plain-text results with the Gson 
`JsonSyntaxException`, the result without a `body` field with a null body, and 
the array, number, invalid JSON and POJO events on the old messages. The 6 that 
pass are the String and Map events, no body, and the results with a `body` 
field, a null `body` field and no result, which behave as before.
   - Module tests: 36 run, 0 failures, 5 skipped (the functional tests that 
need a real FunctionGraph account).
   
   ## Docs and generated files
   
   - Component doc: new "Message body" section on the event and on the body 
after the call.
   - `@BodyTypes`: `producerIn` now lists map, text, bytes, stream and file and 
says that another JSON value fails; `producerOut` describes the body field or 
the whole result. The regenerated `hwcloud-functiongraph.json` and the doc are 
copied to the catalog.
   - Upgrade guide entry for 4.24: the whole result is given when there is no 
`body` field.
   
   ## Not changed
   
   - A top-level JSON array or scalar as the event is still rejected. Sending 
one would need a body that the typed SDK request does not take (for example a 
`Map` that also implements the SDK `SdkSerializable` to write raw JSON), and 
the API documents the event as an object. **Open question:** is that worth 
doing, or is the clear error enough?
   - A POJO is not marshalled to JSON (it fails unless its text is JSON). 
Marshalling it with Jackson would be a new feature; the doc says to marshal it 
first.
   
   Main only, as the other fixes of the CAMEL-24961 follow-ups.
   
   _Claude Code on behalf of Andrea Cosentino (oscerd)_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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