From 7c3f67752bf6b7e9fcc6506cd2800eddfaa155c8 Mon Sep 17 00:00:00 2001 From: Marcos Piccinini Date: Sat, 30 May 2009 06:53:33 -0300 Subject: [PATCH] Ruby 1.9: Fix Hash.from_xml typecast when parsing escaped attributes --- .../active_support/core_ext/hash/conversions.rb | 2 +- activesupport/test/core_ext/hash_ext_test.rb | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 2a34874..6ec4202 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -154,7 +154,7 @@ class Hash case value.class.to_s when 'Hash' if value['type'] == 'array' - child_key, entries = value.detect { |k,v| k != 'type' } # child_key is throwaway + child_key, entries = value.detect { |k,v| k != 'type' && ['Hash', 'Array', 'NilClass'].member?(v.class.to_s) } # child_key and attributes is throwaway if entries.nil? || (c = value['__content__'] && c.blank?) [] else diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index ece5466..979ecb8 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -645,7 +645,25 @@ class HashToXmlTest < Test::Unit::TestCase assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["rsp"]["photos"]["photo"] end - + + def test_single_record_from_xml_with_escaped_attributes + topic_xml = <<-EOT + + + 740829 + feature + + + EOT + + expected_topic_hash = { + :id => 740829, + :story_type => "feature" + }.stringify_keys + + assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["stories"][0] + end + def test_all_caps_key_from_xml test_xml = <<-EOT -- 1.6.3.1