diff options
author | Yves Senn <yves.senn@garaio.com> | 2012-11-12 14:33:20 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@garaio.com> | 2012-11-12 16:06:08 +0100 |
commit | 78dca351037c5f34971bdf0dd7d0b8fc9b9bdeb4 (patch) | |
tree | fc3a9536930132ae531f8e442359ff57f40dc236 /activesupport/test | |
parent | 2f3d81c764a8b527299be8c33c64814431f83482 (diff) | |
download | rails-78dca351037c5f34971bdf0dd7d0b8fc9b9bdeb4.tar.gz rails-78dca351037c5f34971bdf0dd7d0b8fc9b9bdeb4.tar.bz2 rails-78dca351037c5f34971bdf0dd7d0b8fc9b9bdeb4.zip |
`#as_json` isolates options when encoding a hash. Closes #8182
Setting options in a custom `#as_json` method had side effects.
Modifications of the `options` hash leaked outside and influenced
the conversion of other objects contained in the hash.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/json/encoding_test.rb | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 7ed71f9abc..5bb2a45c87 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -22,6 +22,15 @@ class TestJSONEncoding < ActiveSupport::TestCase end end + class CustomWithOptions + attr_accessor :foo, :bar + + def as_json(options={}) + options[:only] = %w(foo bar) + super(options) + end + end + TrueTests = [[ true, %(true) ]] FalseTests = [[ false, %(false) ]] NilTests = [[ nil, %(null) ]] @@ -248,6 +257,15 @@ class TestJSONEncoding < ActiveSupport::TestCase assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json) end + def test_to_json_should_not_keep_options_around + f = CustomWithOptions.new + f.foo = "hello" + f.bar = "world" + + hash = {"foo" => f, "other_hash" => {"foo" => "other_foo", "test" => "other_test"}} + assert_equal(%({"foo":{"foo":"hello","bar":"world"},"other_hash":{"foo":"other_foo","test":"other_test"}}), hash.to_json) + end + def test_struct_encoding Struct.new('UserNameAndEmail', :name, :email) Struct.new('UserNameAndDate', :name, :date) |