Error when using specific adapter on first calls
cbliard opened this issue · 1 comments
cbliard commented
Environment
Using multi_json version 1.15.0 on ruby 3.1.2
Issue
When calling load
or dump
with a specific adapter on first call, it fails with a NoMethodError: undefined method 'key?' for nil:NilClass
error:
[1] pry(main)> MultiJson.dump({abc: "def"}, adapter: :json_gem)
NoMethodError: undefined method `key?' for nil:NilClass
cache.key?(key) ? cache[key] : write(cache, key, &block)
^^^^^
from /home/cbliard/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb:12:in `fetch'
[2] pry(main)> MultiJson.load('{"abc": "def"}', adapter: :json_gem)
NoMethodError: undefined method `key?' for nil:NilClass
cache.key?(key) ? cache[key] : write(cache, key, &block)
^^^^^
from /home/cbliard/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb:12:in `fetch'
If calling it without the adapter
parameter, then with it, it works:
[1] pry(main)> MultiJson.load('{"abc": "def"}')
=> {"abc"=>"def"}
[2] pry(main)> MultiJson.load('{"abc": "def"}', adapter: :json_gem)
=> {"abc"=>"def"}
[3] pry(main)> MultiJson.dump({abc: "def"}, adapter: :json_gem)
=> "{\"abc\":\"def\"}"
The error occurs because the cache MultiJson::OptionsCache
is somehow not initialized yet when calling load
with an adapter parameter.
Workaround
Calling MultiJson::OptionsCache.reset
before anything else can make it work.
[1] pry(main)> MultiJson.load('{"abc": "def"}', adapter: :json_gem)
NoMethodError: undefined method `key?' for nil:NilClass
cache.key?(key) ? cache[key] : write(cache, key, &block)
^^^^^
from /home/cbliard/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb:12:in `fetch'
[2] pry(main)> MultiJson::OptionsCache.reset
=> {}
[3] pry(main)> MultiJson.load('{"abc": "def"}', adapter: :json_gem)
=> {"abc"=>"def"}
djberg96 commented
Can confirm, this happens with multi_json 1.15.0 with ruby 3.1.3 on my Mac. I tried it with "oj".