Skip to content

Commit

Permalink
Merge pull request rails#9111 from jsomara/3-0-json-fix
Browse files Browse the repository at this point in the history
Fix rails#8832 - Parse '{"person":[]}' JSON/XML as {'person' => []}.
  • Loading branch information
tenderlove committed Jan 30, 2013
2 parents 20c3b4b + f20b598 commit 10513d2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion actionpack/lib/action_dispatch/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,12 @@ def deep_munge(hash)
hash.each do |k, v|
case v
when Array
if v.size > 0 && v.all?(&:nil?)
hash[k] = nil
next
end
v.grep(Hash) { |x| deep_munge(x) }
v.compact!
hash[k] = nil if v.empty?
when Hash
deep_munge(v)
end
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/dispatch/request/json_params_parsing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def teardown
end

test "nils are stripped from collections" do
assert_parses(
{"person" => []},
"{\"person\":[]}", { 'CONTENT_TYPE' => 'application/json' }
)
assert_parses(
{"person" => nil},
"{\"person\":[null]}", { 'CONTENT_TYPE' => 'application/json' }
Expand Down
3 changes: 3 additions & 0 deletions actionpack/test/dispatch/request/xml_params_parsing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def assert_parses(expected, xml)
assert_parses(
{"hash" => { "person" => nil} },
"<hash><person type=\"array\"><person nil=\"true\"/></person></hash>")
assert_parses(
{"hash" => { "person" => []} },
"<hash><person type=\"array\"></person></hash>")
assert_parses(
{"hash" => { "person" => ['foo']} },
"<hash><person type=\"array\"><person>foo</person><person nil=\"true\"/></person>\n</hash>")
Expand Down

0 comments on commit 10513d2

Please sign in to comment.