This project is archived and is in readonly mode.

#6406 new
Christopher Thompson

ActiveRecord::XmlSerializer fails with has_one types

Reported by Christopher Thompson | February 10th, 2011 @ 06:05 PM

This problem exists in Rails 2.3.10. Code inspection in 3-0-stable shows the problem also exists there, but I have not verified this. Below, I am discussing Rails 2.3.10.

ActiveRecord::XmlSerializer#add_associations does not properly deal with :has_one relationships. It fails to add the type of the associated data. Worse, if the container object uses STI, the :has_one associated data will claim to be the type of the container object.

Consider for example:

class Container < ActiveRecord::Base
  has_many :sti_outer_objs
  
  def dump
    to_xml({:include=>{:sti_outer_objs=>{:include=>{:sti_inner_obj=>{}}}}})
  end
end

class StiOuterObj < ActiveRecord::Base
  belongs_to :container
  has_one :sti_inner_obj
end

class StiOuterDerivedObj < StiOuterObj
end

class StiInnerObj < ActiveRecord::Base
  belongs_to :sti_outer_obj
end

class StiInnerDerivedObj < StiInnerObj
end

Now, use the following hierarchy:

container -> [sti_outer_derived] -> sti_inner_derived

Calling container.dump will show that the generated XML claims that sti_inner_derived's type is "StiOuterDerived".

The fix is simple and straightforward; we have to calculate the type of the :has_one data. Attached is a patch against Rails-2.3.10 which accomplishes this and solves the problem.

The patch does not apply against Rails 3.x, but I believe the modification is trivial.

I am happy to provide any additional information required.

No comments found

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages