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.

JSON serialization is broken

#5529

JSON serialization is broken in 3.0.0 when I try to serialize a hash which includes an ActiveModel object and passing no-nil options parameter, like so:

options = {}
{:model => model_obj, :some_hash => sub_hash}.to_json(options)

The result is that sub_hash object is serialized as an empty hash and options parameter is modified to contain:

options = {:only => [], :except =[]}

The reason is that activemodel/lib/active_model/serialization.rb#serializable_hash modifies passed parameter, which it shouldn't.

Reported by Kent Sibilev · September 2nd, 2010 @ 06:32 AM

State: resolved
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. José Valim
    José Valim
    • Milestone set to 3.x
    • Assigned user cleared.
    • Importance changed from to Low

    Patch with tests please.

    September 2nd, 2010 @ 06:36 AM

  2. Kent Sibilev
    Kent Sibilev

    I've just sent a pull request with my proposed patch. The test to reproduce the problem is below:

    
    require 'rubygems'
    require 'active_model'
    require 'active_support/core_ext'
    require 'active_support/json'
    
    class User
      include ActiveModel::Serialization
      include ActiveModel::Serializers::JSON
    
      attr_accessor :name
    
      def attributes
        @attributes ||= {'name' => 'nil'}
      end
    end
    
    u = User.new
    u.name = 'kent'
    
    json = { :user => u, :x => { :name => 1 } }.to_json
    puts json # => {"user":{"user":{"name":"kent"}},"x":{"name":1}}
    
    options = {}
    json = { :user => u, :x => { :name => 1 } }.to_json( options )
    
    puts json # => {"user":{"user":{"name":"kent"}},"x":{}}
    p options # => {:except=>[], :only=>[]}
    

    My ignorance prevents me from implementing proper test cases. Sorry.

    September 2nd, 2010 @ 07:39 AM

  3. José Valim
    José Valim

    So could you please create a patch from your pull request and attach it here? It will allow other people to create a test case for your patch so it can be finally applied.

    September 2nd, 2010 @ 07:45 AM

  4. Kent Sibilev
    Kent Sibilev

    Here we go. The diff is attached.

    September 2nd, 2010 @ 07:57 AM

  5. David Trasbo
    David Trasbo

    FYI: I'm going to start making a test for this now.

    September 2nd, 2010 @ 01:13 PM

  6. David Trasbo
  7. David Trasbo
    David Trasbo
    • Assigned user set to José Valim

    September 2nd, 2010 @ 02:49 PM

  8. Adrian
    Adrian

    Hello,

    Im having trouble overriding the as_json method in one of my models,

    Is this issue related to my problem?

    Thanks

    September 3rd, 2010 @ 04:34 AM

  9. David Trasbo
    David Trasbo

    Adrian,

    Maybe, maybe not - I can't tell. Why don't you hop into #railsbridge at irc.freenode.org and ping me there? I'm dtrasbo.

    September 3rd, 2010 @ 01:18 PM

  10. Adrian
    Adrian

    Thanks David, I'll do that

    September 4th, 2010 @ 02:43 PM

  11. David Trasbo
    David Trasbo

    Awesome. FYI, I'm in there now if you have time.

    September 4th, 2010 @ 02:53 PM

  12. Jeff Kreeftmeijer
  13. José Valim
    José Valim

    Kent, could you please rebase your patch? Sorry but it no longer applies.

    October 11th, 2010 @ 11:46 PM

  14. Kent Sibilev
    Kent Sibilev

    You can close this ticket, since it looks like the required changes have been submitted already with another patch.

    Thanks.

    October 12th, 2010 @ 04:20 PM

  15. José Valim
    José Valim
    • State changed from new to resolved

    October 12th, 2010 @ 04:33 PM