This project is archived and is in readonly mode.
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
-
Pratik December 20th, 2008 @ 07:19 PM
- 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!
-
nofxx May 28th, 2009 @ 05:34 PM
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.
-
nofxx May 28th, 2009 @ 05:40 PM
Sorry, formatting...
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<stories type=\"array\" total=\"16\" count=\"16\">\n <story>\n... "
can't typecast "36"
-
Jeff Kreeftmeijer November 8th, 2010 @ 08:55 AM
- Importance changed from to Low
Automatic cleanup of spam.
-
Santiago Pastorino February 9th, 2011 @ 12:31 AM
- 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.
-
Santiago Pastorino February 9th, 2011 @ 12:31 AM
- State changed from open to stale
-
Adrian March 27th, 2011 @ 06:23 PM
- 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.
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>
People watching this ticket
Attachments
Referenced by
- 6689 Docs for ActiveRecord::Locking::Pessimistic don't make sense This is addressed in GH #1032 and fixed in #3b18e900e8fc3...