Skip to content

Commit

Permalink
Fix that empty collections should be treated as empty arrays regardle…
Browse files Browse the repository at this point in the history
…ss of whitespace for Hash#from_xml (closes rails#10255) [adamj]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8202 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Nov 25, 2007
1 parent ccebac9 commit 4d96ece
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Fix that empty collections should be treated as empty arrays regardless of whitespace for Hash#from_xml #10255 [adamj]

* Time#time_with_datetime_fallback, Time#to_datetime, Date#to_datetime and String#to_datetime honor Ruby's default calendar reform setting. #10201 [Geoff Buesing]

* Change Time and DateTime #end_of_month to return last second of month instead of beginning of last day of month. Closes #10200 [Geoff Buesing]
Expand Down
26 changes: 13 additions & 13 deletions activesupport/lib/active_support/core_ext/hash/conversions.rb
Expand Up @@ -174,20 +174,9 @@ def from_xml(xml)
def typecast_xml_value(value)
case value.class.to_s
when 'Hash'
if value.has_key?("__content__")
content = value["__content__"]
if parser = XML_PARSING[value["type"]]
if parser.arity == 2
XML_PARSING[value["type"]].call(content, value)
else
XML_PARSING[value["type"]].call(content)
end
else
content
end
elsif value['type'] == 'array'
if value['type'] == 'array'
child_key, entries = value.detect { |k,v| k != 'type' } # child_key is throwaway
if entries.nil?
if entries.nil? || (c = value['__content__'] && c.blank?)
[]
else
case entries.class.to_s # something weird with classes not matching here. maybe singleton methods breaking is_a?
Expand All @@ -199,6 +188,17 @@ def typecast_xml_value(value)
raise "can't typecast #{entries.inspect}"
end
end
elsif value.has_key?("__content__")
content = value["__content__"]
if parser = XML_PARSING[value["type"]]
if parser.arity == 2
XML_PARSING[value["type"]].call(content, value)
else
XML_PARSING[value["type"]].call(content)
end
else
content
end
elsif value['type'] == 'string' && value['nil'] != 'true'
""
# blank or nil parsed values are represented by nil
Expand Down
11 changes: 11 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -549,6 +549,17 @@ def test_empty_array_from_xml
assert_equal expected_blog_hash, Hash.from_xml(blog_xml)
end

def test_empty_array_with_whitespace_from_xml
blog_xml = <<-XML
<blog>
<posts type="array">
</posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => []}}
assert_equal expected_blog_hash, Hash.from_xml(blog_xml)
end

def test_array_with_one_entry_from_xml
blog_xml = <<-XML
<blog>
Expand Down

0 comments on commit 4d96ece

Please sign in to comment.