aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-11-13 02:26:01 -0800
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-11-13 02:26:01 -0800
commit3e53fe635c6e7b82609219422458574e2f74b551 (patch)
tree8cb4f4c7cb47541dcbe683c5e986dd0c0fdbdc08
parent618923280048928aff50eed4fe9a9b08f9ebad72 (diff)
parentbe79632b96d6fe6b844c99561ce96f540e98cae0 (diff)
downloadrails-3e53fe635c6e7b82609219422458574e2f74b551.tar.gz
rails-3e53fe635c6e7b82609219422458574e2f74b551.tar.bz2
rails-3e53fe635c6e7b82609219422458574e2f74b551.zip
Merge pull request #8199 from senny/backport_8182
backport #8185, `#as_json` isolates options when encoding a hash.
-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 749441e343..d15c341b9f 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.10 (unreleased)
+* `#as_json` isolates options when encoding a hash. [Backport #8185]
+ Fix #8182
+
+ *Yves Senn*
+
* Handle the possible Permission Denied errors atomic.rb might trigger due to
its chown and chmod calls. [Backport #8027]
diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
index 91e4f07c6c..bd2f909ca9 100644
--- a/activesupport/lib/active_support/json/encoding.rb
+++ b/activesupport/lib/active_support/json/encoding.rb
@@ -61,7 +61,7 @@ module ActiveSupport
# hashes and arrays need to get encoder in the options, so that 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 a9590f164f..6cbf956be2 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -22,6 +22,15 @@ class TestJSONEncoding < Test::Unit::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) ]]
@@ -239,6 +248,15 @@ class TestJSONEncoding < Test::Unit::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)