opensearch-project / opensearch-java

Java Client for OpenSearch
Apache License 2.0
126 stars 190 forks source link

[BUG] Headers provided in the request are not respected in the RestClientTransport #1234

Open bcallender opened 1 month ago

bcallender commented 1 month ago

What is the bug?

If you create a Request with headers, then send the request via the RestClientTransport, those headers are ignored, and not included in the request that is actually sent to opensearch.

How can one reproduce the bug?

Create any request to any endpoint and specify a header

    Request request =
            Requests.builder()
                    .endpoint(endpoint)
                    .headers(List.of(Map.entry("osd-xsrf", "true")))
                    .method(RestRequest.Method.POST.name())
                    .json(objectMapper.writeValueAsString(patternPayload))
                    .build();

send that request via the OpensearchGenericClient's execute() function, with the transport configured to use the RestClientTransport.

You will find that the sent message does not include this header, making it impossible to do things like create index patterns programmatically using the dashboards API. The issue is in prepareLowLevelRequest(), which does not merge the headers from the incoming request object, only creating new ones. I'm not sure if the same behavior occurs with the other transport clients.

What is the expected behavior?

If I provide headers in my request, those headers should also appear in the request made to Opensearch and should not be lost.

What is your host/environment?

Mac OS 14.6 -- Running Opensearch 2.19.0 in Kubernetes, and using the latest version of the opensearch-java library (2.15.0)

Do you have any screenshots?

N/A

Do you have any additional context?

Add any other context about the problem.

Xtansia commented 1 month ago

Thank you for catching this @bcallender! Would you be interested in contributing a fix for this?

bcallender commented 1 month ago

I can take a look into this, sure. I'll need some time to get up to speed, would be my first contribution.