Permalink
Browse files

Merge pull request #9111 from jsomara/3-0-json-fix

Fix #8832 - Parse '{"person":[]}' JSON/XML as {'person' => []}.
  • Loading branch information...
2 parents 20c3b4b + f20b598 commit 10513d2eb9892c9e2bfe0ae04c9c16165c5c1544 @tenderlove tenderlove committed Jan 30, 2013
@@ -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
@@ -32,6 +32,10 @@ def teardown
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' }
)
@@ -42,6 +42,9 @@ def assert_parses(expected, xml)
{"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>")
end

0 comments on commit 10513d2

Please sign in to comment.