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

Ticket #7685 (closed defect: fixed)

Opened 2 years ago

Last modified 1 year ago

from_xml does not mirror to_xml behavior vis-a-vis arrays

Reported by: jake007 Assigned to: core
Priority: normal Milestone: 1.2.4
Component: ActiveSupport Version: 1.2.1
Severity: normal Keywords: ActiveSupport,from_xml,to_xml,singularize,XmlSimple
Cc:

Description

Hash::to_xml correctly converts an array within a hash into a series of xml nodes. E.g.

h = {"songs"=>[{"title"=>"foo"}, {"title"=>"bar"}]}
puts h.to_xml

generates

<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <songs>
    <song>
      <title>foo</title>
    </song>
    <song>
      <title>bar</title>
    </song>
  </songs>
</hash>

to_xml correctly singularizes the key name to label the child nodes. This is done in conversions.rb line# 44. However, converting this xml back to a hash via from_xml does not recognize the node songs as an array of sub-nodes:

s = <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <songs>
    <song>
      <title>foo</title>
    </song>
    <song>
      <title>bar</title>
    </song>
  </songs>
</hash>
EOS
h = Hash.from_xml(s)['hash']
puts h.inspect

gives

{"songs"=>{"song"=>[{"title"=>"foo"}, {"title"=>"bar"}]}}

which is not the same.

As a solution, either:

1. to_xml should give the option to not add the singularized subnodes, such that the first example would generate:

<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <songs>
    <title>foo</title>
  </songs>
  <songs>
    <title>bar</title>
  </songs>
</hash>

or,

2. The from_xml method would recognize the plural-singulars and "compress" the resulting hash so that it's

{"songs"=>[{"title"=>"foo"}, {"title"=>"bar"}]}

rather than

{"songs"=>{"song"=>[{"title"=>"foo"}, {"title"=>"bar"}]}}

Jakub Roth

Change History

05/07/07 15:19:04 changed by cch1

I agree with Jakub -this inconsistency is frustrating. I would prefer to see the the encoding (to_xml) remain the same and change the decoding (from_xml). I've blogged about this problem here: http://cho.hapgoods.com/wordpress/?p=129

06/22/07 02:06:24 changed by kamal

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

Resolved in [7074]