Skip to content

buildCompatibilityHeaders() forcibly adds compatibility headers even when API versioning is not enabled #1460

@odiaseo

Description

@odiaseo

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=8 but the server expects plain application/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

  1. Use elastic/elasticsearch-php client to connect to an Elasticsearch 8.10 or 8.19 server.
  2. Make any request (e.g., search).
  3. Observe that the client automatically sends compatible-with=8 headers, causing a 400 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=1 should 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_APIVERSIONING variable, or
  • Add a client configuration option to disable buildCompatibilityHeaders() for 8.x servers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions