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

   Fixes [CAMEL-26232](https://issues.apache.org/jira/browse/CAMEL-26232), 
found while adding the catalog body types (CAMEL-24961).
   
   ## 1. A built request in the body is not used as the request
   
   **Was:** the producer picks the operation from a built request in the body 
(`IndexRequest`, `GetRequest`, `SearchRequest` and six more), but the type 
converter only accepted the builders. A built `IndexRequest` was written as the 
document itself, with a generated id and the index of the option, and a built 
`UpdateRequest` became the document of a new update request, which failed 
without the id header. The other seven failed with "Wrong body type", and there 
was no converter to an `MsearchRequest.Builder` at all.
   
   **Now:** each converter that takes a request builder also takes the built 
request and turns it back into its builder with `rebuild()`, so every request 
type the producer resolves is sent as it is, including with `useScroll`. A new 
converter to `MsearchRequest.Builder` takes an `MsearchRequest` (or its 
builder). The headers and options do not change a built request, which matches 
what already happens to a builder in the body: the producer gets a builder body 
without calling the converter. The "Wrong body type" messages list the built 
requests.
   
   ## 2. A JSON search body keeps only its query
   
   **Was:** `toSearchRequestBuilder` kept only the `query` member of a JSON 
text or Map body, so `size`, `from`, `sort`, aggregations, `_source` and the 
rest were dropped without a warning. A Map body whose `query` value is not a 
Map failed with a `ClassCastException`.
   
   **Now:** the whole body goes to `SearchRequest.Builder.withJson`, which 
keeps every member of the search request and rejects an unknown one with an 
error that names it. A JSON object with a single member other than `query`, 
such as `{"match": {...}}`, is still taken as the query alone, as before. A Map 
body is written as JSON first, so a `query` value that is a Jackson `JsonNode` 
works, and one that is not a query fails with the JSON error of the client. The 
`size` and `from` headers or options, when set, still override those of the 
body, as they did.
   
   ## 3. DeleteIndex ignores the index name in a String body
   
   **Was:** a `String` body was required but its text was not used: the index 
came from the `CamelElasticsearchIndexName` header or the `indexName` option 
only, and with neither the request went out with no index (`DELETE /`), which 
the server rejects.
   
   **Now:** the header or option still wins; when neither is set, the `String` 
body names the index. With no name at all, the request fails before it is sent, 
with "Missing required property 'DeleteIndexRequest.index'".
   
   ## 4. Index/Update with no body or a File: unclear result
   
   **Was:** a null body reached the converter as the message itself (Camel 
tries the message when the body is null), which was then serialized as the 
document and failed with a Jackson error. A `java.io.File` was written as a 
JSON string of its path, a `Path` as a URI string, and the file of the file 
component as a JSON object of its properties.
   
   **Now:** `Index` and `Update` fail with an `IllegalArgumentException` that 
says the body cannot be null, before anything is sent. A `java.io.File`, a 
`java.nio.file.Path` or the file of the file component (`WrappedFile`) is 
opened through the type converters and read as the JSON of the document, like a 
stream body. The `file` carrier is added to the body types of `Index` and 
`Update`.
   
   ## Tests
   
   - `ElasticsearchProducerBodyTest` (new, 18 tests) runs the producer against 
a JDK `HttpServer` that records the requests and answers them as Elasticsearch 
does: each of the nine built requests, the DeleteIndex name from the body and 
from the option, a null body for Index and Update, a `File`, a `Path` and a 
`GenericFile` for Index, a `File` for Update, and a `String` id for GetById.
   - `ElasticsearchActionRequestConverterTest` (10 new tests): a JSON search 
body with size, from, sort, aggregations, `_source` and `track_total_hits`, a 
search body without `query`, a query alone as JSON and as a Map, a Map search 
body, a Map whose `query` is a `JsonNode`, the size and from headers over the 
body, and the DeleteIndex name from the body, from the header, and missing.
   - With the fix reverted, 22 of the 28 new tests fail for the issue's reasons 
("Wrong body type", a generated id, the Jackson error on the message, the path 
written as the document, the `ClassCastException`, a `null` index name). The 6 
that pass check behaviour that does not change: the DeleteIndex option and 
header win over the body, a query alone as JSON and as a Map, the size and from 
headers over the body, and a `String` id for GetById.
   - Module: 51 tests, 0 failures. The integration tests need a container and 
were not run.
   
   ## Docs and generated files
   
   - The `@BodyTypes` of the endpoint: the `file` carrier for Index and Update, 
and the description of the built requests, the search body and the DeleteIndex 
name.
   - `elasticsearch-component.adoc`: the operations table and the search 
example. The catalog copies of the JSON and the doc are updated.
   - The generated type converter loader has the new `MsearchRequest.Builder` 
converter.
   
   ## Not changed
   
   - A built request is sent as it is, and so is a builder in the body (the 
producer reads a builder body without the converter). The converter branches 
that change a builder, the id header of an `IndexRequest.Builder` and the index 
of a `SearchRequest.Builder` without one, are only reached when the converter 
is called directly; they are left as they are.
   - A body is taken as a query alone when it is an object with a single member 
other than `query`; before, any body without a `query` member was. So `{"size": 
0, "aggs": {...}}` now works as a search request, while a search body with a 
single member that is not a query, such as `{"aggs": {...}}` alone, is still 
taken as a query and fails, as before.
   - The `size` and `from` headers or options override those of the body, as 
they did; the alternative was to let the body win.
   - A file inside the List of a Bulk is still written as it was, as the issue 
names Index and Update only.
   - A null body for the other operations still fails with "Wrong body type", 
and DeleteIndex still needs a `String` body even when the header names the 
index.
   
   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