Browse files

`#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.
  • Loading branch information...
1 parent 2f3d81c commit 78dca351037c5f34971bdf0dd7d0b8fc9b9bdeb4 @senny senny committed Nov 12, 2012
View
5 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*
View
2 activesupport/lib/active_support/json/encoding.rb
@@ -65,7 +65,7 @@ def options_for(value)
# they can detect circular references.
options.merge(:encoder => self)
else
- options
+ options.dup
end
end
View
18 activesupport/test/json/encoding_test.rb
@@ -22,6 +22,15 @@ def as_json(options)
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 @@ def test_enumerable_should_pass_encoding_options_to_children_in_to_json
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)

0 comments on commit 78dca35

Please sign in to comment.