From e4a4287c971f6e61f59815988f5fc69176cb14de Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Fri, 1 Sep 2006 16:09:18 +0000 Subject: [PATCH] Fix cases where empty xml nodes weren't being translated to nil in Hash.create_from_xml [Rick Olson] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4895 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 5 ++++ .../core_ext/hash/conversions.rb | 2 +- activesupport/test/core_ext/hash_ext_test.rb | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 1d15c27fef..cf66ee470e 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,10 @@ *SVN* +* Fix cases where empty xml nodes weren't being translated to nil in Hash.create_from_xml [Rick Olson] + + # => { :type => 'date' } # WRONG + # => nil # RIGHT + * Tighten rescue clauses. #5985 [james@grayproductions.net] * Inflections: don't singularize -ies plurals. [foamdino@gmail.com, Mark Van Holstyn] diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 38992f2926..fdc5a165b0 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -101,7 +101,7 @@ module ActiveSupport #:nodoc: else content end else - value.empty? || value['nil'] == 'true' ? nil : value.inject({}) do |h,(k,v)| + (value.blank? || value['type'] || value['nil'] == 'true') ? nil : value.inject({}) do |h,(k,v)| h[k] = typecast_xml_value(v) h end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index f0a50ec5fe..d2372dcc32 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -339,6 +339,30 @@ class HashToXmlTest < Test::Unit::TestCase assert_equal expected_topic_hash, Hash.create_from_xml(topic_xml)["topic"] end + def test_single_record_from_xml_with_nil_values + topic_xml = <<-EOT + + + + + + + + + EOT + + expected_topic_hash = { + :title => nil, + :id => nil, + :approved => nil, + :written_on => nil, + :viewed_at => nil, + :parent_id => nil + }.stringify_keys + + assert_equal expected_topic_hash, Hash.create_from_xml(topic_xml)["topic"] + end + def test_multiple_records_from_xml topics_xml = <<-EOT