Permalink
Browse files

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

  • Loading branch information...
ndbroadbent committed Jan 9, 2013
1 parent 11f5deb commit 43a732eb6e635f0f0e73051b94a5647db49ff2cd
@@ -253,9 +253,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
@@ -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' }
@@ -42,6 +42,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>")

1 comment on commit 43a732e

vjt commented on 43a732e Jan 10, 2013

👍!

Please sign in to comment.