aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2013-11-22 16:01:22 -0800
committerJeremy Kemper <jeremy@bitsweat.net>2013-11-22 16:01:22 -0800
commit5542dff88a8581675c9324c8184cc80df99e3325 (patch)
treea4092093382343b02c1f4eb7f3ab8f2932cb9cd6 /activesupport
parent818b362c1db0b1103d6489dbd6f1f56e645a34e3 (diff)
parent663c059d4a043e0baabb69e60436bb849eb620fb (diff)
downloadrails-5542dff88a8581675c9324c8184cc80df99e3325.tar.gz
rails-5542dff88a8581675c9324c8184cc80df99e3325.tar.bz2
rails-5542dff88a8581675c9324c8184cc80df99e3325.zip
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
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/object/json.rb4
-rw-r--r--activesupport/test/json/encoding_test.rb16
2 files changed, 18 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/json.rb b/activesupport/lib/active_support/core_ext/object/json.rb
index 5157b0402f..1a1fa1eedd 100644
--- a/activesupport/lib/active_support/core_ext/object/json.rb
+++ b/activesupport/lib/active_support/core_ext/object/json.rb
@@ -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:
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 39da760bf2..00f43be6d4 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -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)