Permalink
Browse files

Fix #8832 - Parse '{"person":[]}' JSON/XML as {'person' => []}.

  • Loading branch information...
1 parent b875be0 commit f20b59882675ec2fab28fd92d8ceecedd978bd97 @ndbroadbent ndbroadbent committed with Jordan OMara Jan 9, 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
@zdennis
zdennis Feb 11, 2013 Contributor

Isn't this line still problematic? Shouldn't this be set to an empty array?

+ 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 f20b598

Please sign in to comment.