From 1c02184e2c96c926222ad4f6c3119c102017677d Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Fri, 12 Jun 2026 15:32:00 +0100 Subject: [PATCH] Fixes NoMethodError when Content-Type lacks compatible-with parameter Fixes #2973 --- .../lib/elasticsearch/api/utils.rb | 2 +- .../spec/unit/ndjson_endpoints/bulk_spec.rb | 60 ++++++++++++++----- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/elasticsearch-api/lib/elasticsearch/api/utils.rb b/elasticsearch-api/lib/elasticsearch/api/utils.rb index 65591d403..1f34f7bc6 100644 --- a/elasticsearch-api/lib/elasticsearch/api/utils.rb +++ b/elasticsearch-api/lib/elasticsearch/api/utils.rb @@ -178,7 +178,7 @@ def rescue_from_not_found(&block) def update_ndjson_headers!(headers, client_headers) current_content = client_headers.keys.find { |c| c.match?(/content-?_?type/i) } || 'content-type' current_accept = client_headers.keys.find { |c| c.match?(/accept/i) } || 'accept' - version = client_headers[current_content].match(/compatible-with=([0-9]+)/)[1] || 9 + version = client_headers[current_content].match(/compatible-with=([0-9]+)/)&.[](1) || 9 headers.merge!( { diff --git a/elasticsearch-api/spec/unit/ndjson_endpoints/bulk_spec.rb b/elasticsearch-api/spec/unit/ndjson_endpoints/bulk_spec.rb index d775c4e12..12a7064d1 100644 --- a/elasticsearch-api/spec/unit/ndjson_endpoints/bulk_spec.rb +++ b/elasticsearch-api/spec/unit/ndjson_endpoints/bulk_spec.rb @@ -33,14 +33,14 @@ it 'does not override headers' do allow(client) .to receive(:perform_request) - .with( - Elasticsearch::API::HTTP_POST, - '_bulk', - {}, - {}, - expected_headers, - { endpoint: 'bulk' } - ) + .with( + Elasticsearch::API::HTTP_POST, + '_bulk', + {}, + {}, + expected_headers, + { endpoint: 'bulk' } + ) expect(client.bulk(body: {})).to be_a Elasticsearch::API::Response end end @@ -110,15 +110,43 @@ it 'does not override version in headers' do allow(client) .to receive(:perform_request) - .with( - Elasticsearch::API::HTTP_POST, - '_bulk', - {}, - {}, - expected_headers, - { endpoint: 'bulk' } - ) + .with( + Elasticsearch::API::HTTP_POST, + '_bulk', + {}, + {}, + expected_headers, + { endpoint: 'bulk' } + ) expect(client.bulk(body: {}, headers: { x_custom: 'Custom header' })).to be_a Elasticsearch::API::Response end end + + context 'when using application/x-ndjson as a content-type header' do + let(:client) do + Elasticsearch::Client.new( + url: 'http://localhost:9200', + transport_options: { + headers: { 'Content-Type' => 'application/x-ndjson' } + } + ) + end + + it 'doesnt error when using bulk' do + allow(client) + .to receive(:perform_request) + .with( + Elasticsearch::API::HTTP_POST, + '_bulk', + {}, + {}, + { + 'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9', + 'accept' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9' + }, + { endpoint: 'bulk' } + ) + expect(client.bulk(body: {})).to be_a Elasticsearch::API::Response + end + end end