1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

When Array#as_json and Hash#as_json are called without options, they

should also call #as_json on the children without options (instead of
nil)
This commit is contained in:
Godfrey Chan 2013-11-22 11:01:40 -08:00
parent 34c08d2ead
commit 663c059d4a
2 changed files with 18 additions and 2 deletions

View file

@ -164,7 +164,7 @@ end
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 @@ class Hash
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:

View file

@ -32,6 +32,12 @@ class TestJSONEncoding < ActiveSupport::TestCase
end
end
class OptionsTest
def as_json(options = :default)
options
end
end
class HashWithAsJson < Hash
attr_accessor :as_json_called
@ -332,6 +338,16 @@ class TestJSONEncoding < ActiveSupport::TestCase
"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)