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

Already on GitHub? Sign in to your account

Class methods (dump/load) rely on state #55

Closed
troessner opened this Issue Dec 13, 2010 · 1 comment

Comments

Projects
None yet
2 participants

I use JSON.dump and JSON.load a lot in my application. 99% of the time it's working as expected, but in a cucumber feature I get:

undefined method merge' for #JSON::Ext::Generator::State:0xb63993e0 (NoMethodError)

~/.bundler/ruby/1.8/my_lib-54ae46e6989b/lib/my_lib/event_type.rb:34:in store!'

~/.bundler/ruby/1.8/my_lib-54ae46e6989b/lib/my_lib/event_type.rb:23:in create'

./app/controllers/tenant/event_types_controller.rb:16:in create'

./features/step_definitions/web_steps.rb:29

./features/step_definitions/web_steps.rb:14:in with_scope'

./features/step_definitions/web_steps.rb:28:in /^(?:|I )press "([^"])"(?: within "([^"])")?$/'
features/event_types.feature:34:in And I press "Create"

This is what "store!" looks like:

def store!

$redis.set(@redis_key, JSON.dump(@Constraints))

end

This method is called a lot in my application, and I never had any problems with json dumping, except for in my cucumber feature.

The funny thing now is, that there is nothing wrong with "@Constraints" - in some tests before "@Constraints" hash value is the very same as in my failing cucumber spec and looks something like that (yes, no symbols, that's intentional):

{ 'required' => %w!item_id checkout_type!,

'type' => { 'item_id' => 'Integer' } }

To make this point clearer:

I debug my features with ruby-debug -> I have a working feature which calls "store!" -> I check the hash value of "@Constraints" -> let the feature successfully pass -> I step to my next (failing) feature -> I check the hash value of "@Constraints" -> it's the same as before -> I let the feature pass -> it fails with the error from above.

I don't know what the exact problem here is, but apparently JSON.dump / load does rely on some kind of state (what the error message hints at as well).

Not sure what's going wrong here, but as far as I can see, this is some kind of bug - a class method that may be called in any possible context (especially something like "dump()" / "load()") shouldn't rely on any kind of state.

Owner

flori commented Aug 29, 2011

This might have been caused by rails trying to treat the state object like a Hash instance, which fails because #merge was missing. I have added a merge method to the state object, so the following would work:

JSON.state.new.merge(:max_nesting => 5).max_nesting # => 5

@flori flori closed this Aug 29, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment