oscerd opened a new pull request, #28951: URL: https://github.com/apache/camel/pull/28951
Fixes [CAMEL-26260](https://issues.apache.org/jira/browse/CAMEL-26260), found while adding the catalog body types (CAMEL-24961). ## 1. A byte[] or stream body fails **Was:** `AlibabaEventBridgeUtils.toCloudEvent` accepted a `CloudEvent`, a `Map` or a `String`, and threw `IllegalArgumentException: Exchange body must be a CloudEvent, Map or JSON string` for anything else. The JSON body of an HTTP or Kafka consumer (a `byte[]` or a stream), or a file, failed. The security example of the doc worked around it with `convertBodyTo(String.class)`. **Now:** a body that converts to bytes, such as a `byte[]`, an `InputStream`, a `File` or a `GenericFile`, is read as text in the charset of the exchange (the `CamelCharsetName` header or property, UTF-8 by default). It is then handled like a `String`: it is the JSON text of the data of the event, and the source, the type and the bus come from the options or headers. This works for the items of a `List` body (a batch) too. The issue proposes to convert other bodies to String with the type converter. The body goes through bytes first, with the type converter, on purpose: the String conversion falls back to `toString()`, so a POJO would be published as `Order[id=123]`. Collections that are not a `List` and arrays other than `byte[]` are left out as well, because the type converter turns them into bytes: an empty `Set` would have been published as an event with empty data. A POJO, a `Set`, or no body still fail with an `IllegalArgumentException`. The message now says that the body is null, or names the type of the body and lists what is accepted. The catalog body types (`producerIn`) now list bytes, stream and file. ## Tests - New `PutEventsBodyTest` (10 tests) with a Mockito `EventBridgeClient`, which captures the published `CloudEvent` list. It checks the data, source, type and bus of the event for a `String`, a `byte[]`, an `InputStream`, a `File` and a `GenericFile` holding JSON with non-ASCII text. It also covers ISO-8859-1 bytes with the `CamelCharsetName` header, a `List` of two `byte[]`, no body, a POJO and an empty `Set`. - With the `src/main` changes reverted, 9 of the 10 tests fail. 6 fail on the old exception: bytes, stream, file, `GenericFile`, charset and the `List` of bytes. 3 fail only on the new message: no body, POJO and empty `Set`, which the old code also rejected, with "Exchange body must be a CloudEvent, Map or JSON string". The one that passes is the `String` body. - With only the collection and array check removed, the empty `Set` test fails: the `Set` is published. - Module tests: 36 tests, 0 failures. ## Docs and generated files - Component doc: the "Publish event with String / JSON body" example says that bytes, a stream or a file work too, and which charset is used. The security example no longer needs `convertBodyTo(String.class)`. It now sets the `eventSource` and `eventType` options it was missing: without them, its String body always failed with "Event source, type and event bus name are required". - The `@BodyTypes` of `AlibabaEventBridgeEndpoint`; the regenerated `alibaba-eventbridge.json` and the doc are copied into the catalog. - Upgrade guide: none. A body that failed now works, and the bodies that still fail only get a clearer message. ## Not changed - An empty `String`, an empty `byte[]` or an empty stream is still published as an event with empty data, as an empty `String` was before. - The JSON text is not parsed or validated, as for a `String` before. 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]
