提交 5542dff8 编写于 作者: J Jeremy Kemper

Merge pull request #13007 from chancancode/dont_call_as_json_with_nil

Don't call #as_json on children of Array and Hash with nil when no arguments are passed
......@@ -164,7 +164,7 @@ def as_json(options = nil) #:nodoc:
class Array
def as_json(options = nil) #:nodoc:
map { |v| v.as_json(options && options.dup) }
map { |v| options ? v.as_json(options.dup) : v.as_json }
end
def encode_json(encoder) #:nodoc:
......@@ -187,7 +187,7 @@ def as_json(options = nil) #:nodoc:
self
end
Hash[subset.map { |k, v| [k.to_s, v.as_json(options && options.dup)] }]
Hash[subset.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }]
end
def encode_json(encoder) #:nodoc:
......
......@@ -32,6 +32,12 @@ def as_json(options={})
end
end
class OptionsTest
def as_json(options = :default)
options
end
end
class HashWithAsJson < Hash
attr_accessor :as_json_called
......@@ -332,6 +338,16 @@ def test_to_json_should_not_keep_options_around
"other_hash" => {"foo"=>"other_foo","test"=>"other_test"}}, ActiveSupport::JSON.decode(hash.to_json))
end
def test_hash_as_json_without_options
json = { foo: OptionsTest.new }.as_json
assert_equal({"foo" => :default}, json)
end
def test_array_as_json_without_options
json = [ OptionsTest.new ].as_json
assert_equal([:default], json)
end
def test_struct_encoding
Struct.new('UserNameAndEmail', :name, :email)
Struct.new('UserNameAndDate', :name, :date)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册