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

Ticket #7307 (closed defect: fixed)

Opened 2 years ago

Last modified 1 year ago

[PATCH] mixing :include and :methods in options hash passed to #to_xml

Reported by: jwilger Assigned to: nzkoz
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: xml :methods :include verified
Cc:

Description

Let's say I have the following model classes defined:

class Foo < ActiveRecord::Base
  has_many :bars

  def some_method
    "I am some_method"
  end
end

class Bar < ActiveRecord::Base
  belongs_to :foo
end

If I try to run

Foo.find( 1 ).to_xml( :include => [ :bars ], :methods => [ :some_method ] )

there is a problem, because the options hash passed to Foo#to_xml is passed down to Bar#to_xml when it serializes the associated objects. In general, that's probably what you want -- the problem is that Bar#some_method is not defined, and so you get a NoMethodError when the serialization tries to call #some_method on the Bar instances.

I wonder if it would be a good idea to change the ActiveRecord::XmlSerialization::XmlSerializer#serializable_method_attributes method from

def serializable_method_attributes
  Array(options[:methods]).collect { |name| MethodAttribute.new(name.to_s, @record) }
end

to something like

def serializable_method_attributes
  valid_methods = Array(options[:methods]).reject { |name| !...@record.respond_to? name }
  valid_methods.collect { |name| MethodAttribute.new(name.to_s, @record) }
end

Obviously, the danger here is that you do not get an exception if you specify a method in the options that is not defined anywhere in the object graph being serialized. Personally, I think that would be a good trade-off. Otherwise I've found that I have to write a custom #to_xml method in many of my model classes just to remove unknown methods from the :methods option. Better to take care of that once at the root.

Attachments

xml_serialization_ignores_undefined_methods_200701221649.diff (1.1 kB) - added by jwilger on 01/23/07 00:59:31.
patch to ignore undefined methods passed in :methods option to #to_xml
dont_call_unsupported_methods_on_included_associations.diff (2.4 kB) - added by manfred on 06/27/07 15:35:27.
My solution for this problem.

Change History

01/23/07 00:59:31 changed by jwilger

  • attachment xml_serialization_ignores_undefined_methods_200701221649.diff added.

patch to ignore undefined methods passed in :methods option to #to_xml

01/24/07 01:28:44 changed by nzkoz

  • owner changed from core to nzkoz.
  • status changed from new to assigned.

03/14/07 22:12:38 changed by josh

  • keywords changed from xml :methods :include to xml :methods :include verified.
  • version set to edge.

(in reply to: ↑ description ) 03/15/07 19:23:41 changed by mef

+1

03/15/07 19:25:37 changed by leftist

+1

06/27/07 15:35:27 changed by manfred

  • attachment dont_call_unsupported_methods_on_included_associations.diff added.

My solution for this problem.

06/27/07 15:37:15 changed by manfred

I was having the same problem as jwilger, I noticed there weren't even tests for the positive case. I've added a test for the positive and negative case.

06/30/07 03:32:47 changed by nzkoz

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

Applied in [7156]

thanks guys