-
Notifications
You must be signed in to change notification settings - Fork 979
Description
The bug
In the elastic/elasticsearch-php library, the function buildCompatibilityHeaders($headers) in Elastic\Elasticsearch\Traits\EndpointTrait automatically adds compatible-with headers to all requests. This happens even when API versioning is supposed to be disabled.
- This breaks requests when connecting to Elasticsearch 8.x servers (tested on 8.10 and 8.19) if the client sends
Content-Type: application/vnd.elasticsearch+json; compatible-with=8but the server expects plainapplication/json. - There is no option in the client configuration to disable this behaviour.
Expected behavior
When API versioning is disabled (environment variable ELASTIC_CLIENT_APIVERSIONING=1 is not set or false), the client should not add compatible-with headers and should use normal JSON headers (Content-Type: application/json, Accept: application/json).
Current behavior
buildCompatibilityHeaders() overrides headers regardless of the API versioning setting, forcing compatible-with=8 and causing 400 Bad Request errors with:
{
"error": {
"root_cause": [
{
"type": "media_type_header_exception",
"reason": "Invalid media-type value on headers [Accept, Content-Type]"
}
],
"type": "media_type_header_exception",
"reason": "Invalid media-type value on headers [Accept, Content-Type]",
"caused_by": {
"type": "status_exception",
"reason": "A compatible version is required on both Content-Type and Accept headers if either one has requested a compatible version. Accept=null, Content-Type=application/vnd.elasticsearch+json; compatible-with=8"
},
"status": 400
}
}Steps to reproduce
- Use
elastic/elasticsearch-phpclient to connect to an Elasticsearch 8.10 or 8.19 server. - Make any request (e.g.,
search). - Observe that the client automatically sends
compatible-with=8headers, causing a400 Bad Request.
Environment
- Elasticsearch server: 8.10 / 8.19
- elastic/elasticsearch-php: latest 8.19 branch
- PHP version: 8.3
Additional context
- Documentation mentions that setting
ELASTIC_CLIENT_APIVERSIONING=1should enable compatibility mode. However, this environment variable is ignored in the code, so there’s no way to disable forced compatibility headers.
Proposed solution
- Honor the
ELASTIC_CLIENT_APIVERSIONINGvariable, or - Add a client configuration option to disable
buildCompatibilityHeaders()for 8.x servers.