Fix serializer.as_json(nil)

This is the form that ActiveSupport 3.1 Object#to_json invokes.
This commit is contained in:
Gabe da Silveira 2013-12-30 12:36:18 -06:00
parent e81406a248
commit 36d92804ed
2 changed files with 14 additions and 0 deletions

View File

@ -337,6 +337,7 @@ module ActiveModel
# Returns a json representation of the serializable
# object including the root.
def as_json(options={})
options ||= {}
if root = options.fetch(:root, @options.fetch(:root, root_name))
@options[:hash] = hash = {}
@options[:unique_values] = {}

View File

@ -1449,4 +1449,17 @@ class SerializerTest < ActiveModel::TestCase
}
}, post_serializer.as_json)
end
def test_as_json_with_nil_options
user = User.new
user_serializer = DefaultUserSerializer.new(user, {})
# ActiveSupport 3.1 Object#to_json generates this downstream call
assert_equal({
:default_user => {
:first_name => "Jose",
:last_name => "Valim"
}
}, user_serializer.as_json(nil))
end
end