httparty icon indicating copy to clipboard operation
httparty copied to clipboard

Parse crashes when extra attributes on type="array"

Open aishfenton opened this issue 15 years ago • 5 comments

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) }

aishfenton avatar Sep 29 '11 17:09 aishfenton

Can it be fixed by using a different multi xml parser?

jnunemaker avatar Oct 05 '11 15:10 jnunemaker

Hi, Have tried with Rexml and Nokogiri, and am getting the same issue.

aishfenton avatar Oct 18 '11 18:10 aishfenton

Thanks for that monkey patch in that line, Aish! That works for me.

fisherwebdev avatar Oct 25 '11 23:10 fisherwebdev

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.

fisherwebdev avatar Feb 01 '12 05:02 fisherwebdev

Sweet. Safe to say this is not an httparty issue, but rather a multi_xml issue, so I'm closing the issue.

jnunemaker avatar Feb 01 '12 14:02 jnunemaker