Parse crashes when extra attributes on type="array"
Hi,
I'm doing a get on a service that has some XML like the following:
<cars type="array" anything="anything">
<car>
<id>134744</id>
</car>
<car>
<id>134744</id>
</car>
</cars>
Any extra attributes after type="array" (i.e. anything="anything") cause httparty's code that converts it to a hash to crash. The offending lines are these:
def MultiXml.typecast_xml_value(value)
case value
when Hash
if value['type'] == 'array'
_, entries = value.detect { |k, v| k != 'type' }
Now I think the service I'm getting shouldn't have these extra attributes on a type="array" element, but it would be nice if httparty handled this case more gracefully.
For the moment, I've monkey patched one line above so that the parser only pulls out the attribute that contains the array elements (means the extra attributes aren't parsed), but at least it works without throwing a cryptic error.
_, entries = value.detect { |k, v| k != 'type' && v.is_a?(Array) }
Can it be fixed by using a different multi xml parser?
Hi, Have tried with Rexml and Nokogiri, and am getting the same issue.
Thanks for that monkey patch in that line, Aish! That works for me.
Hey check this out:
There were some bugs recently resolved in multi_xml, particularly with regard to arrays. This bug fix solves this issue in httparty.
I suggest you point bundler to the GitHub repo to get the very latest version of multi_xml, in your Gemfile like this:
gem 'multi_xml', :git => 'https://github.com/sferik/multi_xml'
gem 'ox'
gem 'httparty'
Then where you are going to use httparty (for example, in your Sinatra server) you would do this:
require 'bundler/setup'
Note that with this setup, multi_xml will not show up in your "gem list" output, but it will work.
Sweet. Safe to say this is not an httparty issue, but rather a multi_xml issue, so I'm closing the issue.