aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-11-12 17:53:17 -0800
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-11-12 17:53:17 -0800
commitd5c4370ef58cacb2c019160fb7bb79ffe1c7a67e (patch)
tree2e1962c48129cf9c1c179f88f344553a913cc090 /activesupport
parent85cc7122bab1cfec661fdfe3bf4738d9fba55399 (diff)
parent78dca351037c5f34971bdf0dd7d0b8fc9b9bdeb4 (diff)
downloadrails-d5c4370ef58cacb2c019160fb7bb79ffe1c7a67e.tar.gz
rails-d5c4370ef58cacb2c019160fb7bb79ffe1c7a67e.tar.bz2
rails-d5c4370ef58cacb2c019160fb7bb79ffe1c7a67e.zip
Merge pull request #8185 from senny/8182_as_json_options_stick_around
`#as_json` isolates options when encoding a hash. Closes #8182
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md5
-rw-r--r--activesupport/lib/active_support/json/encoding.rb2
-rw-r--r--activesupport/test/json/encoding_test.rb18
3 files changed, 24 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index dc84b44dec..24989fc244 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* `#as_json` isolates options when encoding a hash.
+ Fix #8182
+
+ *Yves Senn*
+
* Deprecate Hash#diff in favor of MiniTest's #diff. *Steve Klabnik*
* Kernel#capture can catch output from subprocesses *Dmitry Vorotilin*
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index f65c831e04..7a5c351ca8 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -65,7 +65,7 @@ module ActiveSupport
# they can detect circular references.
options.merge(:encoder => self)
else
- options
+ options.dup
end
end
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)