From 0de09cf3e5d10adb69231e26f6ba5afcbdb5e2e0 Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Thu, 24 Apr 2025 11:47:36 +0100 Subject: [PATCH 1/2] [Gem] Updates setting content-type, accept to not overwrite user set headers --- elasticsearch/lib/elasticsearch.rb | 14 +++-- elasticsearch/spec/unit/headers_spec.rb | 69 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/elasticsearch/lib/elasticsearch.rb b/elasticsearch/lib/elasticsearch.rb index 3e55285ad9..fc54bae1c8 100644 --- a/elasticsearch/lib/elasticsearch.rb +++ b/elasticsearch/lib/elasticsearch.rb @@ -180,11 +180,15 @@ def set_user_agent!(arguments) end def set_content_type!(arguments) - headers = { - 'content-type' => 'application/vnd.elasticsearch+json; compatible-with=9', - 'accept' => 'application/vnd.elasticsearch+json; compatible-with=9' - } - set_header(headers, arguments) + headers = {} + user_headers = arguments&.[](:transport_options)&.[](:headers) + unless user_headers&.keys&.detect { |h| h =~ /content-?_?type/ } + headers['content-type'] = 'application/vnd.elasticsearch+json; compatible-with=9' + end + unless user_headers&.keys&.detect { |h| h =~ /accept/ } + headers['accept'] = 'application/vnd.elasticsearch+json; compatible-with=9' + end + set_header(headers, arguments) unless headers.empty? end def set_header(header, arguments) diff --git a/elasticsearch/spec/unit/headers_spec.rb b/elasticsearch/spec/unit/headers_spec.rb index 48a072b2fc..3751c74488 100644 --- a/elasticsearch/spec/unit/headers_spec.rb +++ b/elasticsearch/spec/unit/headers_spec.rb @@ -52,4 +52,73 @@ client.search(headers: param_headers) end end + + context 'when accept header is changed' do + let!(:client) do + described_class.new( + host: 'http://localhost:9200', + transport_options: { headers: instance_headers } + ).tap do |client| + client.instance_variable_set('@verified', true) + end + end + let(:instance_headers) do + { accept: 'application/json' } + end + + it 'performs the request with the header' do + connection_headers = client.transport.connections.connections.first.connection.headers + expect(connection_headers['Accept']).to eq 'application/json' + expect(connection_headers['Content-Type']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + + expect_any_instance_of(Faraday::Connection) + .to receive(:run_request) + .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } + client.search + end + end + + context 'when content-type header is changed' do + let!(:client) do + described_class.new( + host: 'http://localhost:9200', + transport_options: { headers: instance_headers } + ).tap do |client| + client.instance_variable_set('@verified', true) + end + end + let(:instance_headers) do + { content_type: 'application/json' } + end + + it 'performs the request with the header' do + connection_headers = client.transport.connections.connections.first.connection.headers + expect(connection_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + expect(connection_headers['Content-Type']).to eq 'application/json' + + expect_any_instance_of(Faraday::Connection) + .to receive(:run_request) + .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } + client.search + end + end + + context 'when no header is set, uses v9 content-type and accept' do + let!(:client) do + described_class.new(host: 'http://localhost:9200').tap do |client| + client.instance_variable_set('@verified', true) + end + end + + it 'performs the request with the header' do + expected_headers = client.transport.connections.connections.first.connection.headers + expect(expected_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + expect(expected_headers['Content-Type']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' + + expect_any_instance_of(Faraday::Connection) + .to receive(:run_request) + .with(:get, 'http://localhost:9200/_search', nil, expected_headers) { OpenStruct.new(body: '') } + client.search + end + end end From 8cb8992b924285aace9e7239c0dc09d32d31a28f Mon Sep 17 00:00:00 2001 From: Fernando Briano Date: Thu, 24 Apr 2025 12:12:32 +0100 Subject: [PATCH 2/2] Bumps version to 9.0.2 and updates CHANGELOG --- CHANGELOG.md | 4 ++++ elasticsearch-api/lib/elasticsearch/api/version.rb | 2 +- elasticsearch/elasticsearch.gemspec | 2 +- elasticsearch/lib/elasticsearch/version.rb | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe77d46529..5bf67d8bf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ **See the full release notes on the official documentation website: https://www.elastic.co/docs/release-notes/elasticsearch/clients/ruby** +# 9.0.2 + +- Udpates setting 'Accept' and 'Content-Type' headers as to not duplicate or overwrite set headers [#2666](https://github.com/elastic/elasticsearch-ruby/pull/2666). + # 9.0.1 - The request headers were updated for Elasticsearch v9: `compatible-with=9` [#2660](https://github.com/elastic/elasticsearch-ruby/pull/2660). diff --git a/elasticsearch-api/lib/elasticsearch/api/version.rb b/elasticsearch-api/lib/elasticsearch/api/version.rb index cfe3ba7dc9..e80c399f42 100644 --- a/elasticsearch-api/lib/elasticsearch/api/version.rb +++ b/elasticsearch-api/lib/elasticsearch/api/version.rb @@ -17,7 +17,7 @@ module Elasticsearch module API - VERSION = '9.0.1'.freeze + VERSION = '9.0.2'.freeze ES_SPECIFICATION_COMMIT = '52c473efb1fb5320a5bac12572d0b285882862fb'.freeze end end diff --git a/elasticsearch/elasticsearch.gemspec b/elasticsearch/elasticsearch.gemspec index af51fbec94..d626c4b89f 100644 --- a/elasticsearch/elasticsearch.gemspec +++ b/elasticsearch/elasticsearch.gemspec @@ -46,7 +46,7 @@ Gem::Specification.new do |s| s.rdoc_options = ['--charset=UTF-8'] s.required_ruby_version = '>= 2.6' # For compatibility with JRuby 9.3 - s.add_dependency 'elasticsearch-api', '9.0.1' + s.add_dependency 'elasticsearch-api', '9.0.2' s.add_dependency 'elastic-transport', '~> 8.3' s.add_development_dependency 'base64' diff --git a/elasticsearch/lib/elasticsearch/version.rb b/elasticsearch/lib/elasticsearch/version.rb index c745276d84..71afc1f7f1 100644 --- a/elasticsearch/lib/elasticsearch/version.rb +++ b/elasticsearch/lib/elasticsearch/version.rb @@ -16,5 +16,5 @@ # under the License. module Elasticsearch - VERSION = '9.0.1'.freeze + VERSION = '9.0.2'.freeze end