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

Ticket #8566 (new defect)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Complex types with only one key raise an exception

Reported by: foeken Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveResource Version: edge
Severity: normal Keywords:
Cc:

Description

When parsing XML like this:

<clients>
 <client>
   <id>23</id>
   <addresses>
    <address>
      <street>gogostreet</street>
    </address>
  </addresses>
 </client>
</clients>

An exception is raised: ArgumentError: expected an attributes Hash, got "gogostreet"

I fixed the problem by editing the function from_xml_data(data) in connection.rb

I changed it to:

# Manipulate from_xml Hash, because xml_simple is not exactly what we
      # want for ActiveResource.
      def from_xml_data(data)
        case data
          when Hash
            if data.keys.size == 1
              case data.values.first
                when Hash  then [ from_xml_data(data.values.first) ]
                when Array then data[data.keys.first] = from_xml_data(data.values.first)
                else       data
              end
            else
              data.each_key { |key| data[key] = from_xml_data(data[key]) }
              data
            end
          when Array then data.collect { |val| from_xml_data(val) }
          else data
        end
      end

Just changing the data.values.first to data seems to solve this problem, though it was there for a reason probably. Haven't tested it properly to. Take a look.

Attachments

handle_complex_xml_types_in_activeresource.diff (3.1 kB) - added by mmmultiworks on 06/21/07 05:25:42.
Handle Complex Types In ActiveResource
handle_complex_xml_types_in_activeresource_updated.diff (4.9 kB) - added by mmmultiworks on 06/25/07 22:44:43.

Change History

06/21/07 05:25:08 changed by mmmultiworks

Attached is a patch to handle complex type like that described above. The patch however doesn't use the suggested fix. It instead makes a change to instantiate_collection. Also included in the patch are two test cases which test a collection and a single complex type.

06/21/07 05:25:42 changed by mmmultiworks

  • attachment handle_complex_xml_types_in_activeresource.diff added.

Handle Complex Types In ActiveResource

06/21/07 05:26:17 changed by mmmultiworks

  • summary changed from Complex types with only one key raise an exception to [PATCH] Complex types with only one key raise an exception.

06/25/07 15:25:59 changed by BCC

This still fails with this patch:

<patterns>
  <mornings>
    <day>1</day>
  </mornings>
</patterns>

ArgumentError: expected an attributes Hash, got "1"

06/25/07 15:26:19 changed by BCC

With both patches actually.

06/25/07 15:55:12 changed by BCC

As always I've oversimplified my example. This fails:

<patterns>
  <mornings>
    <day>1</day>
    <day>2</day>
  </mornings>
</patterns>

06/25/07 22:44:23 changed by mmmultiworks

I've updated the patch to handle the xml fragment noted above. It makes a change to the load method on ActiveResource::Base to accomplish this.

06/25/07 22:44:43 changed by mmmultiworks

  • attachment handle_complex_xml_types_in_activeresource_updated.diff added.

06/26/07 09:23:37 changed by BCC

An empty list also causes prolems:

<clients></clients>
undefined method `first' for {"clients"=>nil}:Hash

Or should I start a new ticket for this?