oscerd opened a new pull request, #28957: URL: https://github.com/apache/camel/pull/28957
Fixes [CAMEL-26299](https://issues.apache.org/jira/browse/CAMEL-26299), found while adding the catalog body types (CAMEL-24961). ## 1. Every send failed with a NullPointerException **Was:** the producer built each event with `new CloudEvent("/camel", eventType, BinaryData.fromObject(data), null, null)`. azure-core (1.57.0 on main) requires a data format when there is data, so every send failed with `NullPointerException: 'format' cannot be null when 'data' isn't null.` before it reached Azure. No body failed with an NPE too. **Now:** each event is built with the data format that fits the body (azure-core 1.57.0 and azure-messaging-eventgrid 4.31.4 have only this five-argument constructor): - A Map, a POJO or a number: `BinaryData.fromObject` with `CloudEventDataFormat.JSON`, so the data is the JSON object or value. - Text (a String, another CharSequence or a Reader) that is a JSON object or array with nothing after it: `BinaryData.fromString` with `CloudEventDataFormat.JSON`, so it is sent as it is, not quoted. - Other text: a JSON string. The CloudEvent constructor writes the text of a JSON string between quotes without escaping it (azure-core 1.58.1 too), so a quote, a backslash or a line break made the request invalid JSON. Such an event is read with `CloudEvent.fromString` from JSON that the producer writes with the azure-json writer, which keeps the text. - Bytes, an InputStream (also a stream cache), a File, a Path or the GenericFile of a file consumer: read to bytes with the type converter and sent with `BinaryData.fromBytes` and `CloudEventDataFormat.BYTES`, in the `data_base64` field. - No body fails with `InvalidPayloadException` (`getMandatoryBody`), and a null element of a List body with an `IllegalArgumentException`, before anything is sent. ## Tests - New `EventGridProducerTest` (15 tests) sends through a mocked `EventGridPublisherClient` and checks each published event as the JSON the client sends to Event Grid (`CloudEvent.toJson`, read back with azure-json, so invalid JSON fails the test): a Map, a POJO, a number, JSON object text, JSON array text, plain text, text with quotes, a tab, a backslash, a line break and non-ASCII characters, text that starts as JSON but has more after it, a byte[] with 0xff and 0x00, an InputStream, a File, the GenericFile of a file consumer, a List (one event per element, each with its own id), the type, subject, id and time headers, and no body. - With the `src/main` changes reverted, all 15 fail: 14 on the `'format' cannot be null` NPE (the file consumer test because nothing is sent), and the no-body test gets the NPE instead of `InvalidPayloadException`. With only the format added (`fromObject` and `CloudEventDataFormat.JSON` for every body), 8 fail: bytes, stream, file and file consumer are not sent as `data_base64`, and the JSON object text, JSON array text, text with quotes and line breaks, and text with more after the JSON make invalid JSON. - `mvn clean install` of the module: 21 tests (with the 6 of `EventGridComponentTest`), 0 failures. ## Docs and generated files - Component doc: a new "The data of the event" section (how each body is sent, no body, the headers of the event attributes and their defaults). - `@BodyTypes`: the producerIn carriers are now map, pojo, value, text, bytes, stream, file and list, and the description says how each is sent. - Regenerated `azure-eventgrid.json` (body types only), copied into the catalog with the doc. - No upgrade guide entry: every send failed before, so no route can depend on the old behaviour. No option or header changed. ## Not changed - Bytes, a stream or a file that hold JSON, such as the result of `marshal().json()`, are sent as binary data in `data_base64`, as the issue proposes for bytes; the docs say to convert them to a String to send them as JSON. **Open question:** send them as JSON data when they parse as JSON, or when the `Content-Type` header is `application/json`? - No body still fails, as the catalog said, although a CloudEvent without data is valid. **Open question:** publish an event without data when there is no body? - The `datacontenttype` attribute is still not set. - The producer uses azure-json (`com.azure.json`, which `CloudEvent` implements) directly. It comes with azure-core and azure-messaging-eventgrid and is not declared in the module pom, like azure-core. 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]
