Skip to content

Commit

Permalink
Merge pull request #349 from bradley178/master
Browse files Browse the repository at this point in the history
Hash.from_xml chokes on empty CDATA
  • Loading branch information
josevalim committed May 7, 2011
2 parents c515f64 + aba149d commit a6ef255
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def typecast_xml_value(value)
raise "can't typecast #{entries.inspect}" raise "can't typecast #{entries.inspect}"
end end
end end
elsif value['type'] == 'file' || value["__content__"].present? elsif value['type'] == 'file' ||
(value["__content__"] && (value.keys.size == 1 || value["__content__"].present?))
content = value["__content__"] content = value["__content__"]
if parser = ActiveSupport::XmlMini::PARSING[value["type"]] if parser = ActiveSupport::XmlMini::PARSING[value["type"]]
parser.arity == 1 ? parser.call(content) : parser.call(content, value) parser.arity == 1 ? parser.call(content) : parser.call(content, value)
Expand Down
10 changes: 8 additions & 2 deletions activesupport/test/core_ext/hash_ext_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -897,7 +897,13 @@ def test_tag_with_attrs_and_whitespace
hash = Hash.from_xml(xml) hash = Hash.from_xml(xml)
assert_equal "bacon is the best", hash['blog']['name'] assert_equal "bacon is the best", hash['blog']['name']
end end


def test_empty_cdata_from_xml
xml = "<data><![CDATA[]]></data>"

assert_equal "", Hash.from_xml(xml)["data"]
end

def test_xsd_like_types_from_xml def test_xsd_like_types_from_xml
bacon_xml = <<-EOT bacon_xml = <<-EOT
<bacon> <bacon>
Expand Down Expand Up @@ -940,7 +946,7 @@ def test_type_trickles_through_when_unknown


assert_equal expected_product_hash, Hash.from_xml(product_xml)["product"] assert_equal expected_product_hash, Hash.from_xml(product_xml)["product"]
end end

def test_should_use_default_value_for_unknown_key def test_should_use_default_value_for_unknown_key
hash_wia = HashWithIndifferentAccess.new(3) hash_wia = HashWithIndifferentAccess.new(3)
assert_equal 3, hash_wia[:new_key] assert_equal 3, hash_wia[:new_key]
Expand Down

0 comments on commit a6ef255

Please sign in to comment.