Skip to content

Commit

Permalink
Merge pull request rails#1296 from c42engineering/issue636
Browse files Browse the repository at this point in the history
Resubmitting issue rails#636 as a pull request
  • Loading branch information
josevalim committed Jul 11, 2011
2 parents 871d7fa + 83555a5 commit e9f9ce9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def typecast_xml_value(value)
case value.class.to_s
when 'Hash'
if value['type'] == 'array'
_, entries = Array.wrap(value.detect { |k,v| k != 'type' })
_, entries = Array.wrap(value.detect { |k,v| not v.is_a?(String) })
if entries.nil? || (c = value['__content__'] && c.blank?)
[]
else
Expand Down
49 changes: 49 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,55 @@ def test_timezoned_attributes
assert_match %r{<local-created-at type=\"datetime\">1999-02-01T19:00:00-05:00</local-created-at>}, xml
end

def test_multiple_records_from_xml_with_attributes_other_than_type_ignores_them_without_exploding
topics_xml = <<-EOT
<topics type="array" page="1" page-count="1000" per-page="2">
<topic>
<title>The First Topic</title>
<author-name>David</author-name>
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
<author-email-address>[email protected]</author-email-address>
<parent-id nil="true"></parent-id>
</topic>
<topic>
<title>The Second Topic</title>
<author-name>Jason</author-name>
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
<author-email-address>[email protected]</author-email-address>
<parent-id></parent-id>
</topic>
</topics>
EOT

expected_topic_hash = {
:title => "The First Topic",
:author_name => "David",
:id => 1,
:approved => false,
:replies_count => 0,
:replies_close_in => 2592000000,
:written_on => Date.new(2003, 7, 16),
:viewed_at => Time.utc(2003, 7, 16, 9, 28),
:content => "Have a nice day",
:author_email_address => "[email protected]",
:parent_id => nil
}.stringify_keys

assert_equal expected_topic_hash, Hash.from_xml(topics_xml)["topics"].first
end

def test_single_record_from_xml
topic_xml = <<-EOT
<topic>
Expand Down

0 comments on commit e9f9ce9

Please sign in to comment.