Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8537 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] parse XML arrays properly

Reported by: hasmanyjosh Assigned to: technoweenie
Priority: normal Milestone: 1.x
Component: ActiveResource Version: edge
Severity: normal Keywords: xml
Cc:

Description

XML param parsing is slightly rude in that it collapses "degenerate" cases to non-array values. Empty arrays collapse to nil, and an array with one value collapses to just that value. On top of that, you have to traverse an intermediate hash to get to the array values. This patch to ActiveSupport corrects XML array parsing to always generate an array: either an array that's empty, has one value, or has many values. It also removes the intermediate hash.

Before:

Hash.from_xml '<images></images>'
# => {:images => nil}

Hash.from_xml '<images><image>foo.jpg</image></images>'
# => {:images => {:image => "foo.jpg"}}

Hash.from_xml '<images><image>foo.jpg</image><image>bar.jpg</image></images>'
# => {:images => {:image => ["foo.jpg", "bar.jpg"]}}

After:

Hash.from_xml '<images type="array"></images>'
# => {:images => []}

Hash.from_xml '<images type="array"><image>foo.jpg</image></images>'
# => {:images => ["foo.jpg"]}

Hash.from_xml '<images type="array"><image>foo.jpg</image><image>bar.jpg</image></images>'
# => {:images => ["foo.jpg", "bar.jpg"]}

Note that XML is whitespace sensitive, so putting whitespace (like a line break) into an otherwise empty array royally messes things up. So don't do that.

Attachments

xml_arrays_always_parse_to_ruby_arrays.diff (5.2 kB) - added by hasmanyjosh on 06/01/07 00:40:42.
fix xml array parsing
parse_xml_arrays_to_ruby_arrays.diff (12.1 kB) - added by hasmanyjosh on 06/13/07 04:41:28.
new and improved patch, parse xml arrays, and have ARes use them the right way

Change History

06/01/07 00:40:42 changed by hasmanyjosh

  • attachment xml_arrays_always_parse_to_ruby_arrays.diff added.

fix xml array parsing

06/01/07 00:43:41 changed by hasmanyjosh

  • keywords set to xml.

Oh yeah, this also modifies generation of arrays in xml to add a type="array" attribute. This is what allows the proper array parsing.

06/01/07 06:48:13 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [6924]) Added proper handling of arrays (closes #8537) [hasmanyjosh]

06/01/07 17:15:06 changed by technoweenie

  • status changed from closed to reopened.
  • resolution deleted.

Hey Josh, the fix broke ActiveResource:

  1) Error:
test_get_collection(ConnectionTest):
NoMethodError: undefined method `[]' for nil:NilClass
    ./test/connection_test.rb:107:in `test_get_collection'

  2) Failure:
test_get_collection_empty(ConnectionTest) [./test/connection_test.rb:118]:
<nil> expected but was
<"\n">.

  3) Error:
test_get_collection_single(ConnectionTest):
NoMethodError: undefined method `[]' for nil:NilClass
    ./test/connection_test.rb:113:in `test_get_collection_single'

06/05/07 14:10:04 changed by technoweenie

I rolled this changeset back until a patch that works with ActiveResource is available.

06/13/07 04:41:28 changed by hasmanyjosh

  • attachment parse_xml_arrays_to_ruby_arrays.diff added.

new and improved patch, parse xml arrays, and have ARes use them the right way

06/13/07 04:46:09 changed by hasmanyjosh

  • component changed from ActiveSupport to ActiveResource.

Updated the patch. Now fixes how ARes is consuming the XML. Also updated some of the ARes tests, which were unnecessarily brittle, using handcoded XML instead of XML generated by to_xml.

06/13/07 05:16:58 changed by bitsweat

  • owner changed from core to technoweenie.
  • status changed from reopened to new.

06/18/07 00:46:35 changed by cch1

+1 for this patch. I am very frustrated by the current xml array parsing, and this patch addresses both of my big concerns. FWIW, this ticket addresses the problems reported four months ago in ticket #7685.

06/21/07 15:07:19 changed by rick

  • status changed from new to closed.
  • resolution set to fixed.

(In [7074]) Added proper handling of arrays. Closes #8537 [hasmanyjosh]