rails/rails — Issue #3353: Active Support JSON Encoding options_for should not return nil
Author: nuoyan
Created: 2011-10-17 20:17:43+00:00
State: open

In Rails 3.0.10 activesupport-3.0.10/lib/active_support/json/encoding.rb, the options_for method returns "options", which can be nil, if value is not an Array or Hash.

This is problematic because later it passes the nil value back to the encode method:
value.as_json(options_for(value)).

If we define an empty hash as as_json's default optional parameter in the model code, it will break because now it's nil but not empty hash.

Please consider returning empty hash if options is nil in the options_for method, like below:

def options_for(value)
if value.is_a?(Array) || value.is_a?(Hash)
# hashes and arrays need to get encoder in the options, so that they can detect circular references
(options || {}).merge(:encoder => self)
else
options || {}
end
end