Skip to content

Commit

Permalink
Strictly determine if the adapter is oj
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshidajp authored and picandocodigo committed Sep 21, 2023
1 parent ee41b42 commit cd6e15c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/elastic/transport/transport/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def perform_request(method, path, params = {}, body = nil, headers = nil, opts =

# Prevent Float value from automatically becoming BigDecimal when using Oj
load_options = {}
load_options[:mode] = :compat if serializer.is_a?(Serializer::MultiJson) && defined?(::Oj)
load_options[:mode] = :compat if ::MultiJson.adapter.to_s == "MultiJson::Adapters::Oj"

json = serializer.load(response.body, load_options)
end
Expand Down
9 changes: 4 additions & 5 deletions spec/elastic/transport/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1196,15 +1196,16 @@
Elastic::Transport::Client.new(hosts: hosts)
end
before do
# Clear MultiJson's adapter
::MultiJson.instance_variable_set(:@adapter, nil)

expect_any_instance_of(Faraday::Connection).to receive(:run_request) do
Elastic::Transport::Transport::Response.new(200, "{\"score\":1.11111111111111111}", { 'content-type' => 'application/json; charset=UTF-8' })
end
end

context 'when default JSON engine is used' do
after do
# Clear MultiJson's adapter
::MultiJson.instance_variable_set(:@adapter, nil)
end
it 'returns as a Float' do
response = client.perform_request('GET', '/')
score = response.body['score']
Expand All @@ -1219,8 +1220,6 @@
require 'oj'
end
after do
# Clear unnecessary Oj
Object.send(:remove_const, :Oj)
# Clear MultiJson's adapter
::MultiJson.instance_variable_set(:@adapter, nil)
end
Expand Down

0 comments on commit cd6e15c

Please sign in to comment.