Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Hash.from_xml fieldnames uncamelizing

#2334

Calling Hash.from_xml with CamelCase element and attribute names causes the names to be underscored.

From http://www.bluequartz.us/phpBB2/...

xml = %(<blah_blah> 1 2 <dasherized_tag> 3 </dasherized_tag> </blah_blah>)

h = Hash.from_xml(xml)

assert_equal(h, {"blah_blah" => {"JunkOne" => "1", "JunkTwo" => "2", "dasherized_tag" => "3"}})

notice that regardless of the tag name the key is the same string. what rails is currently doing is giving me a result that looks like this: {"blah_blah" => {"junk_one" => "1", "junk_two" => "2", "dasherized_tag" => "3"}}

The bug results from a change to ActiveSupport::CoreExtensions::Hash::Conversions::ClassMethods::unrename_keys which used to be:

h[k.to_s.tr("-", "_")] = unrename_keys(v)

Reported by Gregg Kellogg · March 25th, 2009 @ 07:19 AM

State: resolved
Milestone: 3.x
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: Low

Activity

  1. marktucks
    marktucks

    I tried this in script/console and got the following output?

    xml = %(<blah_blah> 1 2 <dasherized_tag> 3 </dasherized_tag> </blah_blah>) => "<blah_blah> 1 2 <dasherized_tag> 3 </dasherized_tag> </blah_blah>" h = Hash.from_xml(xml) => {"blah_blah"=>" 1 2 "}

    It doesn't seem to have the Junk entries and has removed the <dasherized_tag>s

    March 26th, 2009 @ 09:02 PM

  2. marktucks
    marktucks

    Apologies for the formatting.

    @@@ruby xml = %(<blah_blah> 1 2 <dasherized_tag> 3 </dasherized_tag> </blah_blah>) => "<blah_blah> 1 2 <dasherized_tag> 3 </dasherized_tag> </blah_blah>" h = Hash.from_xml(xml) => {"blah_blah"=>" 1 2 "}

    
    

    March 26th, 2009 @ 09:04 PM

  3. Gregg Kellogg
    Gregg Kellogg

    It would probably help if i used the whole test string:

    xml = %(<blah_blah> 1 2 <dasherized_tag> 3 </dasherized_tag> </blah_blah>)

    March 26th, 2009 @ 11:15 PM

  4. Gregg Kellogg
    Gregg Kellogg

    Oops! It seems that it's getting eaten on the submit. I'll modify the appropriate unit test and upload it.

    March 26th, 2009 @ 11:16 PM

  5. Gregg Kellogg
    Gregg Kellogg

    See if this works.

    @@@ruby xml = %(<blah_blah> 1 2 <dasherized_tag> 3 </dasherized_tag> </blah_blah>)

    
    

    March 26th, 2009 @ 11:19 PM

  6. Gregg Kellogg
    Gregg Kellogg

    Okay, here's an updated activesupport/test/core_ext/hash_ext_test.rb with new test test_single_record_from_xml_with_camel_case

    March 26th, 2009 @ 11:28 PM

  7. Gregg Kellogg
    Gregg Kellogg
    • Assigned user set to Jeremy Kemper

    Seems to have been fixed in commit eb201e64c0b68aee6d0715d44cf48178204c4133 "Fixed Hash#from_xml with keys that are all caps."

    July 8th, 2009 @ 06:13 PM

  8. Denis
    Denis

    The constant changing of Hash#from_xml in re: camelCase tags is very confusing. I filed https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets..., mainly as a way to ask that the correct & intended behavior be documented (and then I'd be happy to add a unit test for it).

    October 14th, 2009 @ 05:52 PM

  9. Mike Grafton
    Mike Grafton

    +1 for what Denis said. Also please see my comment on issue #3377

    December 6th, 2009 @ 06:15 PM

  10. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  11. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:53 PM

  12. David Trasbo