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 chokes on attributes

#1032

Loading development environment (Rails 2.1.0)

Hash.from_xml('<?xml version="1.0" encoding="UTF-8"?>boo') RuntimeError: can't typecast "36"

from /Users/morten/Tmp/xmltest/config/initializers/hash_conversions.rb:207:in `typecast_xml_value'
from /Users/morten/Tmp/xmltest/config/initializers/hash_conversions.rb:233:in `typecast_xml_value'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/inflector.rb:283:in `inject'
from /Users/morten/Tmp/xmltest/config/initializers/hash_conversions.rb:232:in `each'
from /Users/morten/Tmp/xmltest/config/initializers/hash_conversions.rb:232:in `inject'
from /Users/morten/Tmp/xmltest/config/initializers/hash_conversions.rb:232:in `typecast_xml_value'
from /Users/morten/Tmp/xmltest/config/initializers/hash_conversions.rb:186:in `from_xml'
from (irb):1

If you cannot reproduce this, it's due to the order of the keys in the hash, they are unordered in theory, but if the "count" attribute appears later in the hash returned by the SimpleXml parse than the value carrying child, then this passes (although the count attribute is dropped). Example:

Hash.from_xml('<?xml version="1.0" encoding="UTF-8"?>boo') => {"organizations"=>["boo"]}

I propose changing:

child_key, entries = value.detect { |k,v| k != 'type' } # child_key is throwaway

To:

child_key, entries = value.detect { |k,v| k != 'type' && ['Hash', 'Array', 'NilClass'].member?(v.class.to_s) } # child_key and attributes is throwaway

And thus silently drop extra attributes as they're not supported in any case. The proper solution would be full XML support, but that would be out of the scope of this functionality I guess.

We set the count attribute in our to_xml on collections in order to provider a better REST interface where we can display a total count and have people paginate.

Reported by Morten · September 12th, 2008 @ 08:28 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: Low

Activity

  1. Morten
  2. Pratik
    Pratik
    • Assigned user changed from Jeremy Kemper to Pratik
    • State changed from new to incomplete

    Could you please submit a patch/test - http://rails.lighthouseapp.com/p... and we can get this in.

    Thanks!

    December 20th, 2008 @ 07:19 PM

  3. nofxx
    nofxx

    I can confirm the bug and that the patch fixes it.

    Example XML:

    "<?xml version="1.0" encoding="UTF-8"?>\n

    can't typecast "16"

    ONLY on ruby 1.9, 1.8 runs fine w/o patching.

    If it helps, I made it.

    May 28th, 2009 @ 05:34 PM

  4. nofxx
    nofxx

    Sorry, formatting...

    
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stories type=\"array\" total=\"16\" count=\"16\">\n  <story>\n... "
    

    can't typecast "36"

    May 28th, 2009 @ 05:40 PM

  5. Jeremy Kemper
    Jeremy Kemper

    Please include a test case as well.

    May 28th, 2009 @ 08:09 PM

  6. nofxx
    nofxx

    Hi Jeremy,

    Yea, shame on me.. heh. Here we go:

    May 29th, 2009 @ 04:38 AM

  7. nofxx
    nofxx

    Ops, "from_xml" not "to"... fixed:

    May 29th, 2009 @ 06:48 AM

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

    May 4th, 2010 @ 06:48 PM

  9. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 8th, 2010 @ 08:55 AM

  10. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 9th, 2011 @ 12:31 AM

  11. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 9th, 2011 @ 12:31 AM

  12. bingbing
  13. Adrian
    Adrian
    • Assigned user cleared.

    I have just been bitten by this bug. Processing an xml string that contained both attributes and text element values, the attributes were completely whacked, breaking the program. If this flattened out the XML appropriately, it is a superb way to easily decode XML values into a hash that be manipulated and persisted. I know everybody's busy, I just never guessed that a method called Hash.from_xml would create a hash that didn't contain all the XML. v3.0.3.

    March 27th, 2011 @ 06:23 PM