Bug using arrays in XML

5 views
Skip to first unread message

iPhoneCode

unread,
Mar 1, 2009, 7:56:52 PM3/1/09
to ObjectiveResource
Hi folks,

I found a very serious bug and one I think it will take some time to
fix.
The de-serialization from XML in which an element A has an array as
direct child element fails beautifully. In this example, ObjectSupport
fails to map the "old" element to "hobbies", since the array is the
first child element of hobbies.

e.g.
<user>
<username>test</username>
<hobbies>
<old type="array">
<hobby>
<hobby-name>Entrepreneur</hobby-name>
</hobby>
<hobby>
<hobby-name>Developer</hobby-name>
</hobby>
</old>
</hobbies>
</user>

I traced the bug to the file FromXMLElementDelegate.m, method
"didStartElement".

Rodrigo

Josh Vickery

unread,
Mar 2, 2009, 8:10:38 AM3/2/09
to objectiv...@googlegroups.com
Hi Rodrigo,

Thanks for the bug report. I've created a ticket on Lighthouse
(http://yfactorial.lighthouseapp.com/projects/18393-objectiveresource/tickets/58-xml-array-parsing-problem)
to track it. Would you be willing to paste the Rails code that
produces the XML in question either on the ticket or here to the
mailing list?

Josh

iPhoneCode

unread,
Mar 2, 2009, 10:46:51 AM3/2/09
to ObjectiveResource
Hi Josh,

Unfortunately I tested this using a static XML file served by a web
server. The XML above should be enough to reproduce it!

Rodrigo

On Mar 2, 10:10 am, Josh Vickery <vicke...@gmail.com> wrote:
> Hi Rodrigo,
>
> Thanks for the bug report. I've created a ticket on Lighthouse
> (http://yfactorial.lighthouseapp.com/projects/18393-objectiveresource/...)
> to track it. Would you be willing to paste the Rails code that
> produces the XML in question either on the ticket or here to the
> mailing list?
>
> Josh
>

Josh Vickery

unread,
Mar 2, 2009, 10:54:36 AM3/2/09
to objectiv...@googlegroups.com
> Unfortunately I tested this using a static XML file served by a web
> server. The XML above should be enough to reproduce it!
Unfortunately, the parser only works with the standard to_xml support
in Rails. Looking at the XML you posted, I'm not sure how you would
create that in Rails, and it's not clear to me how you would translate
it to Objective-C.

If you paste the Objective-C models along with the XML somewhere we
can look at it, but if the XML is non-standard, it will probably need
custom parser support.

iPhoneCode

unread,
Mar 2, 2009, 12:58:20 PM3/2/09
to ObjectiveResource
Hi Josh,

I disagree. I believe it's a bug since the order of the XML elements
is important. If I add a new element, say <name>blabla</name> before
the array element, it works! If the <name> elements comes after the
array, then ObjectiveSupport crashes.
Why is not possible in ActiveResource to have a resource which has two
arrays inside? In my example the resources <hobbies> could have two
arrays: <old> and <new>.

This example works:

<hobbies>
<name>blabla</name> <====== now deserialization
works!!!!!!
<old type="array">
<hobby>
<hobby-name>Entrepreneur</hobby-name>
</hobby>
<hobby>
<hobby-name>Developer</hobby-name>
</hobby>
</old>
</hobbies>

Rodrigo

Josh Vickery

unread,
Mar 2, 2009, 1:10:23 PM3/2/09
to objectiv...@googlegroups.com
> I disagree. I believe it's a bug since the order of the XML elements
> is important.
I don't mean to claim that it isn't bug. I'm asking for your help in
troubleshooting the problem and recreating it. Unfortunately, the XML
that you've posted isn't enough for me to be able to do that.

>If I add a new element, say <name>blabla</name> before
> the array element, it works! If the <name> elements comes after the
> array, then ObjectiveSupport crashes.

That's interesting. Could you paste your Objective-C models somewhere?
I can't test your XML without them.

> Why is not possible in ActiveResource to have a resource which has two
> arrays inside? In my example the resources <hobbies> could have two
> arrays: <old> and <new>.

It would be a little weird to have an attribute named "hobbies" in
Rails unless it's some sort of association. The XML that you posted
suggested that "hobbies" is a class name, with a single association
named "old" which is composed of "hobbies". If you could write some
Rails code that generates the XML in question that would be very
helpful. The current XML handling code in the project is somewhat
flexible, but it's really only designed to work with resource based
Rails apps that use the default to_xml (de)serialization. If you are
trying to make it parse atypical XML, there is a good chance that it
will require modification to handle those cases.

iPhoneCode

unread,
Mar 2, 2009, 5:13:47 PM3/2/09
to ObjectiveResource
Hi all,

I believe I fixed the bug in my GitHub's clone: git://github.com/rodmaz/objectivesupport.git
Probably there is nicer solution to fix it but I wanted to keep as
much as possible of the current code and still fix the problem. I did
some tests and it worked fine, even in more complexes XML with
arrays.
I only touched the method FromXMLElementDelegate:didStartElement.

Rodrigo

Josh Vickery

unread,
Mar 2, 2009, 6:46:36 PM3/2/09
to objectiv...@googlegroups.com
> I believe I fixed the bug in my GitHub's clone: git://github.com/rodmaz/objectivesupport.git
> Probably there is nicer solution to fix it but I wanted to keep as
> much as possible of the current code and still fix the problem. I did
> some tests and it worked fine, even in more complexes XML with
> arrays.
> I only touched the method FromXMLElementDelegate:didStartElement.

Ahh, I see the issue. The problem exists when you have an array type
attribute two levels deep.

<foo>
<bar>
<bazs type="array">
<baz><name>blat</name></baz>
</bazs>
<bar>
</foo>

Thanks for tracking this down Rodrigo!

I wrote a test to isolate this and added a slightly modified version
of your fix: http://github.com/yfactorial/objectivesupport/commit/f804c5f09e8720899f4826d46dd93e60626e0663

What do you think?

iPhoneCode

unread,
Mar 3, 2009, 7:48:15 AM3/3/09
to ObjectiveResource
hi Josh,

Seems great! Thanks for the prompt reaction!

Rodrigo
> of your fix:http://github.com/yfactorial/objectivesupport/commit/f804c5f09e872089...
>
> What do you think?
Reply all
Reply to author
Forward
0 new messages