Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Deprecation warning in xml_serializer.rb:308

#732

I stumbled across a bug here:

active_record/serializers/xml_serializer.rb:308

I believe type just needs to be changed to class, not really sure why it was type - I hope this is actually a bug =)

I get this error in my rails log:

/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/serializers/xml_serializer.rb:308: warning: Object#type is deprecated; use Object#class

Patch attached

Reported by Bill · July 31st, 2008 @ 03:47 AM

State: invalid
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Bill
  2. Brady Bouchard
    Brady Bouchard

    Assuming your description of the problem is correct, your patch is backward. Changes class -> type rather than type -> class.

    July 31st, 2008 @ 05:29 AM

  3. Pratik
    Pratik
    • State changed from new to incomplete

    Tests missing.

    August 22nd, 2008 @ 02:24 PM

  4. schlucke
    schlucke

    The current test (activerecord/test/cases/xml_serialization_test.rb) didn't need a change nor a new test is needed.

    According to Ruby documentation the usage of Object#type is deprecated and Object#class shoud be used instead. So the provided patch fixes the annoying warning messages in tests and server logs ...

    .../vendor/rails/activerecord/lib/active_record/serializers/xml_serializer.rb:308: warning: Object#type is deprecated; use Object#class

    For me the patch works and didn't break anything. My tests and the Rails tests are all running fine.

    November 30th, 2008 @ 04:08 PM

  5. schlucke
    schlucke
    • Assigned user set to Pratik

    Just upgraded my project to Rails 2.2.2 ... warnings still there.

    December 1st, 2008 @ 07:40 PM

  6. Frederick Cheung
    Frederick Cheung
    • Tag changed from 2.1, activerecord, patch to 2.1, 2.2, activerecord, patch

    I tried your change and it caused multiple test failures because all of the type attributes end up being ActiveRecord::ConnectionAdapters::Column rather then integer/datetime/etc...

    Object.type is deprecated, however the Column object has a type attribute. What's happening here is that the serializer is trying to compute the type for an attribute that has no matching column and so compute_type ends up calling type on nil (which is Object's deprecated type). I'd guess that this is actually an application error.

    December 20th, 2008 @ 03:40 PM

  7. schlucke
    schlucke

    Sorry Frederick, you're right! The ActiveRecord tests really break, i was in a complete wrong branch :( My app also breaks, as you describes with type equal to ActiveRecord::ConnectionAdapters::Column. So the patch didn't work.

    On the other hand the warning is still there in my app and can't figure out why. The warning pops up during a deep nested XML serialization with one HABTM relation (implemented with a join table). Seems that ActiveRecord can't resolve the column types of the join table in this case!?

    I'm a noob in Rails, so I don't know how to fix this in my app or in the rails code. But what I've done is to add a ActiveRecord test that reproduces the ruby warning and fails if type==NilClass.

    
    def test_to_xml_including_has_and_belongs_to_many_with_options
      xml = categories(:general).to_xml(
        :indent  => 0, :skip_instruct => true,
        :include => { :authors => {}, :posts => { :include => :comments } }
      )
      assert !xml.include?(%(<post-id type="NilClass">1</post-id>))
    end
    

    Produces this XML:

    
    <?xml version="1.0"?>
    <category>
    	<categorizations-count type="integer" nil="true"/>
    	<id type="integer">1</id>
    	<name>General</name>
    	<posts type="array"/>
    	<authors type="array">
    		<author>
    			<author-address-extra-id type="integer">2</author-address-extra-id>
    			<author-address-id type="integer">1</author-address-id>
    			<id type="integer">1</id>
    			<name>David</name>
    			<post-id type="NilClass">1</post-id>
    		</author>
    		<author>
    			<author-address-extra-id type="integer" nil="true"/>
    			<author-address-id type="integer" nil="true"/>
    			<id type="integer">2</id>
    			<name>Mary</name>
    			<post-id type="NilClass">2</post-id>
    		</author>
    	</authors>
    </category>
    

    The test is like what I do in my app. Perhaps the include is wrong, or there is really a problem in xml_serializer.

    December 26th, 2008 @ 12:37 PM

  8. Frederick Cheung
    Frederick Cheung

    What do the corresponding tables/relations look like ?

    December 26th, 2008 @ 01:03 PM

  9. schlucke
    schlucke

    Sorry, don't know what you mean?

    The patch to reproduce the warnings is against the current Rails master. I didn't change any tables/relations in the ActiveRecord test suite, so if you apply the patch and run the ActiveRecord tests you'll see what I mean.

    December 26th, 2008 @ 01:16 PM

  10. Frederick Cheung
    Frederick Cheung
    • State changed from incomplete to open

    Ah ok, I mistakenly assumed that test was from your app

    December 26th, 2008 @ 02:32 PM

  11. Frederick Cheung
    Frederick Cheung

    Figured this one out. If anything it's more of a habtm bug than xml_serialization: habtm does a join but (by default) does a select *, so you get the columns from the join table. When you get to the point where your serializing you're serializing these extra attributes but rails doesn't know what their column type is. You could fix this by making your habtms look like

    
    class Foo
      has_and_belongs_to_many :bars, :select => "#{table_name}.*"
    end
    

    December 26th, 2008 @ 11:57 PM

  12. schlucke
    schlucke

    This helps to supress the warning messages, but the types in the XML are still wrong (NilClass). From my app:

    
    <cmdb-applications type="array">
      <cmdb-application>
        <aid>NTE07</aid>
        <cmdb-application-id type="NilClass">33</cmdb-application-id>
        <created-at type="datetime">2008-12-26T15:36:01+01:00</created-at>
        <id type="integer">33</id>
        <partition-id type="NilClass">343</partition-id>
        <updated-at type="datetime">2008-12-26T15:36:01+01:00</updated-at>
      </cmdb-application>
      <cmdb-application>
        <aid>e42aa</aid>
        <cmdb-application-id type="NilClass">34</cmdb-application-id>
    ...
    

    What I don't understand is that only the types of the IDs of the join table are wrong, the type of the column "aid" in this case string, is correct!?

    As all the IDs are not really interesting and necessary in XML output, I can skip them like ...

    
    Partition.to_xml(:except => [ :id, :cmdb_application_id, :cmdb_system_id ], :include => { :cmdb_cluster => {}, :cmdb_applications => {}, :cmdb_systems => {})
    

    But this is only a workaround.

    December 27th, 2008 @ 11:35 AM

  13. Frederick Cheung
    Frederick Cheung

    The join table ids are the only ones wrong because if you ask CmdbApplication 'What's the type of Cmdb_application_id' it says 'I don't know' because the corresponding table has no such column.

    A cleaner workaround (which should quite possibly just be the way things are) is to make the habtm not select those columns.

    December 27th, 2008 @ 11:44 AM

  14. schlucke
    schlucke

    Ahh OK, I understand. So we have two solutions to work around the problem: to_xml with :except and hbtm with :select.

    If the rails default behaviour is "works as designed", I think you can close the ticket now.

    Thank you!

    December 27th, 2008 @ 12:23 PM

  15. Pratik
    Pratik
    • State changed from open to invalid

    January 16th, 2009 @ 05:30 PM