oscerd opened a new pull request, #28954: URL: https://github.com/apache/camel/pull/28954
Fixes [CAMEL-26246](https://issues.apache.org/jira/browse/CAMEL-26246), found while adding the catalog body types (CAMEL-24961). ## 1. A JSON text body is sent as a JSON string **Was:** `CosmosDbConfigurationOptionsProxy.getItem` gave the message body to `createItem`, `upsertItem` and `replaceItem` as it was. The SDK serializes a `String` as a JSON string, so the JSON of an item (what an HTTP or file consumer gives) failed with 400 "Document does not contain an id field". The docs said to unmarshal it first. **Now:** `getItem` parses a `String` or `byte[]` body with Jackson (which the module already uses, from the SDK) into a `Map`, which the SDK writes as a JSON object. A `Map` or a POJO is given to the SDK as it is. JSON that is not valid, that holds more than one value (JSON lines), or that is not an object fails with an `IllegalArgumentException` naming the operation, before Cosmos DB is called. The body after the operation is the item as written, as a `Map`. A `byte[]` body was already sent as JSON by the SDK; it is now parsed the same way, so the body after the operation is a `Map` instead of a `byte[]` (upgrade guide). ## 2. A List body cannot be written: createItems and upsertItems are never used **Was:** a `List` body went to the single-item operations as it was, and Cosmos DB rejected it with 400. `getItems`, `createItems` and `upsertItems` handle a List, but no producer operation calls them. **Now:** a `Collection` or an array body (other than `byte[]`), or a JSON array, fails with an `IllegalArgumentException` before Cosmos DB is called, such as "The body of the createItem operation must be one item, but it is a java.util.ArrayList: send each item in its own exchange, for example with the Split EIP". No bulk operation is added (see Not changed). ## 3. A stream body fails and a File body is stored as its path **Was:** with an `InputStream` body (a `ByteArrayInputStreamCache` once stream caching wraps it), the Jackson of the SDK failed with "No serializer found"; a `File` body was serialized as its path. **Now:** an `InputStream`, a `Reader`, a stream cache, a `File`, a `Path` and a `GenericFile` (any `WrappedFile`) are read with the type converter and parsed as JSON, the same as text. Jackson detects the encoding of the bytes (UTF-8, UTF-16 or UTF-32); a `Reader` is read as characters. ## Tests - New `CosmosDbProducerItemBodyTest` (16 tests). A mocked `CosmosAsyncClient` (Mockito, as in the other unit tests of the module) captures the item given to the SDK, and the default `CosmosItemSerializer` of the SDK shows that the document it writes has the `id`. It covers a JSON String for createItem, upsertItem and replaceItem, a byte[] with non-ASCII UTF-8, an InputStream (a stream cache in the route), a Reader, a File, a Path, a file of the file consumer (GenericFile), a Map and a POJO kept as they are, a List for the three operations, a JSON array, a JSON string, JSON lines, text that is not JSON, bytes that are not UTF-8 (0xff), no body and an empty stream. - With the `src/main` changes reverted, 15 of the 16 new tests fail on their assertions: the item reached the SDK as a String, byte[], ByteArrayInputStreamCache, ReaderCache, File, GenericFile or List, and no exception was raised. The one that passes is the Map and POJO test, which guards that they reach the SDK unchanged. - Module: 52 unit tests, 0 failures. The 22 ITs are skipped without an Azure account. ## Docs and generated files - Component doc: the item operations take a POJO, a `Map`, or the JSON of the item as a String, byte[], stream or file; the body after them; a List or a JSON array fails. The createItem and replaceItem examples no longer need the `unmarshal().json()` step that #28403 added as a workaround. - `@BodyTypes`: producerIn of the item operations adds text, stream and file; their producerOut is map or pojo (no longer bytes). The regenerated `azure-cosmosdb.json` and the doc are copied into the catalog (both copies were identical to the module at the base). - Upgrade guide entry for 4.24: a byte[] body gives a Map after the operation, and a List or a JSON array fails with an `IllegalArgumentException` before Cosmos DB is called. ## Not changed - `createItems` and `upsertItems` of `CosmosDbContainerOperations`, and `getItems` of `CosmosDbConfigurationOptionsProxy`, are kept as they are: they are public methods of public classes, and the ITs call the first two. **Open question:** should they become bulk producer operations (taking a List, ideally with the bulk support of the SDK rather than one request per item), or be deprecated for removal? New operations are a feature, so they are not part of this fix. - JSON is parsed with a plain Jackson `ObjectMapper`: a decimal becomes a double, which is how Cosmos DB stores numbers anyway. 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]
