Skip to content
Open
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
8 changes: 7 additions & 1 deletion lib/elastic/transport/transport/serializer/multi_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ def dump(object, options = {})

private

# multi_json 1.21.0 renamed the top-level constant to `MultiJSON` (keeping a
# deprecated `MultiJson` alias), so the constant's absence indicates a deprecated
# version. Checking the constant rather than `Gem.loaded_specs` also works when
# the gem is loaded without RubyGems activation (e.g. from a `bundle install
# --standalone` bundle or a vendored load path), where `Gem.loaded_specs` is
# empty and the version lookup would raise a `NoMethodError` on `nil`.
def deprecated_gem_version_loaded?
Gem.loaded_specs['multi_json'].version < Gem::Version.create('1.21.0')
!defined?(::MultiJSON)
end
end
end
Expand Down
17 changes: 13 additions & 4 deletions test/unit/serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,27 @@ class Elastic::Transport::Transport::SerializerTest < Minitest::Test
context "Serializer" do

should "use MultiJSON by default" do
if Gem.loaded_specs['multi_json'].version < Gem::Version.create('1.21.0')
::MultiJson.expects(:load)
::MultiJson.expects(:dump)
else
if defined?(::MultiJSON)
::MultiJSON.expects(:parse)
::MultiJSON.expects(:generate)
else
::MultiJson.expects(:load)
::MultiJson.expects(:dump)
end

Elastic::Transport::Transport::Serializer::MultiJson.new.load('{}')
Elastic::Transport::Transport::Serializer::MultiJson.new.dump({})
end

should "work when multi_json is loaded without RubyGems activation" do
# When gems are loaded from a plain $LOAD_PATH (e.g. a `bundle install --standalone`
# bundle or a vendored load path), they are not registered in Gem.loaded_specs.
Gem.stubs(:loaded_specs).returns({})

assert_equal({ 'foo' => 'bar' }, Elastic::Transport::Transport::Serializer::MultiJson.new.load('{"foo":"bar"}'))
assert_equal('{"foo":"bar"}', Elastic::Transport::Transport::Serializer::MultiJson.new.dump({ 'foo' => 'bar' }))
end

end

end