Skip to content

Update headers 9.0.2 #2666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-api/lib/elasticsearch/api/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion elasticsearch/elasticsearch.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
14 changes: 9 additions & 5 deletions elasticsearch/lib/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch/lib/elasticsearch/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
# under the License.

module Elasticsearch
VERSION = '9.0.1'.freeze
VERSION = '9.0.2'.freeze
end
69 changes: 69 additions & 0 deletions elasticsearch/spec/unit/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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