Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport #8185, #as_json isolates options when encoding a hash. #8199

Merged
merged 1 commit into from Nov 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions 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]

Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/json/encoding.rb
Expand Up @@ -61,7 +61,7 @@ def options_for(value)
# 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

Expand Down
18 changes: 18 additions & 0 deletions activesupport/test/json/encoding_test.rb
Expand Up @@ -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) ]]
Expand Down Expand Up @@ -239,6 +248,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)
Expand Down