Skip to content

[9.0][Backport] Updates content type header for 9.x #2660

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 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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: 2 additions & 2 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ steps:
env:
RUBY_VERSION: "{{ matrix.ruby }}"
STACK_VERSION: 9.0.0-SNAPSHOT
ES_YAML_TESTS_BRANCH: main
ES_YAML_TESTS_BRANCH: '9.0'
TRANSPORT_VERSION: "{{ matrix.transport }}"
RUBY_SOURCE: "{{ matrix.ruby_source }}"
TEST_SUITE: "platinum"
Expand Down Expand Up @@ -62,7 +62,7 @@ steps:
RUBY_VERSION: "3.4"
RUBY_SOURCE: "ruby"
TEST_SUITE: serverless
ES_YAML_TESTS_BRANCH: main
ES_YAML_TESTS_BRANCH: '9.0'
QUIET: false
command: ./.buildkite/run-yaml-tests.sh
artifact_paths: "elasticsearch-api/tmp/*"
Expand Down
4 changes: 3 additions & 1 deletion elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def bulk(arguments = {})
body
end

headers.merge!('Content-Type' => 'application/x-ndjson')
headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def msearch(arguments = {})
payload = body
end

headers.merge!('Content-Type' => 'application/x-ndjson')
headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
4 changes: 3 additions & 1 deletion elasticsearch-api/lib/elasticsearch/api/actions/msearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def msearch(arguments = {})
payload = body
end

headers.merge!('Content-Type' => 'application/x-ndjson')
headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def msearch_template(arguments = {})
payload = body
end

headers.merge!('Content-Type' => 'application/x-ndjson')
headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ def find_structure(arguments = {})
body
end

headers.merge!('Content-Type' => 'application/x-ndjson')
headers.merge!({
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
})
Elasticsearch::API::Response.new(
perform_request(method, path, params, payload, headers, request_opts)
)
Expand Down
9 changes: 7 additions & 2 deletions elasticsearch-api/spec/unit/actions/bulk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@
params,
body,
headers,
{:endpoint=>"bulk"}
{ endpoint: 'bulk' }
]
end

let(:headers) { { 'Content-Type' => 'application/x-ndjson' } }
let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}

let(:params) { {} }
let(:url) { '_bulk' }
let(:body) { '' }
Expand Down
8 changes: 7 additions & 1 deletion elasticsearch-api/spec/unit/actions/fleet/msearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@
'_fleet/_fleet_msearch',
{},
{},
{ 'Content-Type' => 'application/x-ndjson' },
headers,
{ endpoint: 'fleet.msearch' }
]
end

let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}

it 'performs the request' do
expect(client_double.fleet.msearch(body: {})).to be_a Elasticsearch::API::Response
end
Expand Down
10 changes: 5 additions & 5 deletions elasticsearch-api/spec/unit/actions/msearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
{}
end

let(:headers) do
{ 'Content-Type' => 'application/x-ndjson' }
end
let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}

let(:client) do
Class.new { include Elasticsearch::API }.new
Expand Down Expand Up @@ -89,7 +91,6 @@
end

context 'when an index is specified' do

let(:url) do
'foo/_msearch'
end
Expand All @@ -115,7 +116,6 @@
end

context 'when multiple indices are specified' do

let(:url) do
'foo,bar/_msearch'
end
Expand Down
11 changes: 5 additions & 6 deletions elasticsearch-api/spec/unit/actions/msearch_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
{}
end

let(:headers) do
{ 'Content-Type' => 'application/x-ndjson' }
end
let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}

let(:url) do
'_msearch/template'
end

context 'when a body is provided as a document' do

let(:body) do
<<-PAYLOAD.gsub(/^\s+/, '')
{"index":"foo"}
Expand All @@ -67,7 +68,6 @@
end

context 'when a body is provided as a string' do

let(:body) do
%Q|{"foo":"bar"}\n{"moo":"lam"}|
end
Expand All @@ -78,7 +78,6 @@
end

context 'when an index is provided' do

let(:url) do
'foo/_msearch/template'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@
'_text_structure/find_structure',
{},
{},
{ 'Content-Type' => 'application/x-ndjson' },
headers,
{ endpoint: 'text_structure.find_structure' }
]
end

let(:headers) {
{
'Content-Type' => 'application/vnd.elasticsearch+x-ndjson; compatible-with=9'
}
}

it 'performs the request' do
expect(client_double.text_structure.find_structure(body: {})).to be_a Elasticsearch::API::Response
end
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-api/spec/yaml-test-runner/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def serverless?

# If we're running in a release branch, download the corresponding branch for tests
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
branch = current_branch.match(/[0-9]\.[0-9]+/)&.[](0) || ENV['ES_YAML_TESTS_BRANCH'] || nil
branch = ENV['ES_YAML_TESTS_BRANCH'] || current_branch.match(/[0-9]\.[0-9]+/)&.[](0) || nil
Elasticsearch::Tests::Downloader::run(tests_path, branch)

runner = Elasticsearch::Tests::TestRunner.new(CLIENT, tests_path, logger)
Expand Down
15 changes: 14 additions & 1 deletion elasticsearch/lib/elasticsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def initialize(arguments = {}, &block)
api_key(arguments) if arguments[:api_key]
setup_cloud(arguments) if arguments[:cloud_id]
set_user_agent!(arguments) unless sent_user_agent?(arguments)
set_content_type!(arguments)
@transport = Elastic::Transport::Client.new(arguments, &block)
end

Expand Down Expand Up @@ -175,9 +176,21 @@ def set_user_agent!(arguments)
if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
user_agent << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}"
end
set_header({ user_agent: user_agent.join('; ') }, 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)
end

def set_header(header, arguments)
arguments[:transport_options] ||= {}
arguments[:transport_options][:headers] ||= {}
arguments[:transport_options][:headers].merge!({ user_agent: user_agent.join('; ') })
arguments[:transport_options][:headers].merge!(header)
end
end

Expand Down