This project is archived and is in readonly mode.

#732 ✓invalid
Bill

Deprecation warning in xml_serializer.rb:308

Reported by Bill | July 31st, 2008 @ 03:47 AM | in 2.x

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

Comments and changes to this ticket

  • Bill

    Bill July 31st, 2008 @ 03:50 AM

    • no changes were found...
  • Brady Bouchard

    Brady Bouchard July 31st, 2008 @ 05:29 AM

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

  • Pratik

    Pratik August 22nd, 2008 @ 02:24 PM

    • State changed from “new” to “incomplete”

    Tests missing.

  • schlucke

    schlucke November 30th, 2008 @ 04:08 PM

    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.

  • schlucke

    schlucke December 1st, 2008 @ 07:40 PM

    • Assigned user set to “Pratik”

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

  • Frederick Cheung

    Frederick Cheung December 20th, 2008 @ 03:40 PM

    • 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.

  • schlucke

    schlucke December 26th, 2008 @ 12:37 PM

    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.

  • Frederick Cheung

    Frederick Cheung December 26th, 2008 @ 01:03 PM

    What do the corresponding tables/relations look like ?

  • schlucke

    schlucke December 26th, 2008 @ 01:16 PM

    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.

  • Frederick Cheung

    Frederick Cheung December 26th, 2008 @ 02:32 PM

    • State changed from “incomplete” to “open”

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

  • Frederick Cheung

    Frederick Cheung December 26th, 2008 @ 11:57 PM

    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
    
  • schlucke

    schlucke December 27th, 2008 @ 11:35 AM

    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.

  • Frederick Cheung

    Frederick Cheung December 27th, 2008 @ 11:44 AM

    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.

  • schlucke

    schlucke December 27th, 2008 @ 12:23 PM

    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!

  • Pratik

    Pratik January 16th, 2009 @ 05:30 PM

    • State changed from “open” to “invalid”

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>

Pages