From 36d92804ed3b799ad6e7ea17824458c13e03d1f0 Mon Sep 17 00:00:00 2001 From: Gabe da Silveira Date: Mon, 30 Dec 2013 12:36:18 -0600 Subject: [PATCH] Fix serializer.as_json(nil) This is the form that ActiveSupport 3.1 Object#to_json invokes. --- lib/active_model/serializer.rb | 1 + test/serializer_test.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 281af425..73b6ba42 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -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] = {} diff --git a/test/serializer_test.rb b/test/serializer_test.rb index 6c7cf7b9..2ef49a87 100644 --- a/test/serializer_test.rb +++ b/test/serializer_test.rb @@ -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