Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse crashes when extra attributes on type="array" #102

Closed
aishfenton opened this issue Sep 29, 2011 · 5 comments
Closed

Parse crashes when extra attributes on type="array" #102

aishfenton opened this issue Sep 29, 2011 · 5 comments

Comments

@aishfenton
Copy link

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) }
@jnunemaker
Copy link
Owner

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

@aishfenton
Copy link
Author

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

@fisherwebdev
Copy link

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

@fisherwebdev
Copy link

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.

@jnunemaker
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants