This project is archived and is in readonly mode.

#1032 ✓stale
Morten

Hash.from_xml chokes on attributes

Reported by Morten | September 12th, 2008 @ 08:28 AM | in 3.x

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.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages