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.

[PATCH] Fix changing ActiveModel include_root_in_json = true to default

#3770

This commit was intended to change the default include_root_in_json setting to true. However, it contained an error and didn't actually set the default to true.

The following IRB session illustrates the issue:

ActiveRecord::Base.include_root_in_json
=> nil
irb(main):002:0> a = Article.new(:title => "AWDWR")
=> #<Article id: nil, title: "AWDWR", created_at: nil, updated_at: nil>
irb(main):003:0> puts a.to_json
{"created_at":null,"title":"AWDWR","updated_at":null}
=> nil
irb(main):004:0> ActiveRecord::Base.include_root_in_json = true
=> true
irb(main):005:0> puts a.to_json
{"article":{"created_at":null,"title":"AWDWR","updated_at":null}}
=> nil

A patch is attached.

Reported by Cody Fauser · January 22nd, 2010 @ 01:06 PM

State: resolved
Milestone: 3.0.2
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Christopher Redinger
    Christopher Redinger

    This patch did not apply cleanly to the current master. Submitting an updated patch that does.

    May 14th, 2010 @ 08:16 PM

  2. Neeraj Singh
    Neeraj Singh

    @Christopher Looks like you have some end of line settings that is marking all the test lines as changed.

    -      assert_match %r{^\{"contact":\{}, json
    -      assert_match %r{"name":"Konata Izumi"}, json
    -      assert_match %r{"age":16}, json
    -      assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
    -      assert_match %r{"awesome":true}, json
    -      assert_match %r{"preferences":\{"shows":"anime"\}}, json
    -    ensure
    -      Contact.include_root_in_json = false
    -    end
    +    json = @contact.to_json
    +
    +    assert_match %r{^\{"contact":\{}, json
    +    assert_match %r{"name":"Konata Izumi"}, json
    +    assert_match %r{"age":16}, json
    +    assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
    +    assert_match %r{"awesome":true}, json
    +    assert_match %r{"preferences":\{"shows":"anime"\}}, json
    

    Also in json_serialization_test.rb has two tests with Contact.include_root_in_json = true . Can you add one test with Contact.include_root_in_json = false just to make sure that without root things work fine.

    May 14th, 2010 @ 08:49 PM

  3. Christopher Redinger
    Christopher Redinger

    Neeraj - nope, those are valid changes. They were wrapped in an unnecessary begin/ensure block. I removed the block, and thus outdented the lines.

    May 15th, 2010 @ 01:39 AM

  4. Neeraj Singh
    Neeraj Singh

    @Christopher my bad. Thanks for the update.

    May 15th, 2010 @ 02:31 AM

  5. Rizwan Reza
    Rizwan Reza
    • Milestone cleared.
    • Tag changed from serialization active_model json include_root_in_json to activemodel, bugmash, json, serialization

    May 15th, 2010 @ 06:28 PM

  6. Michael Koziarski
  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Assigned user set to Santiago Pastorino

    June 7th, 2010 @ 08:11 AM

  8. Repository
    Repository
    • State changed from open to resolved

    June 8th, 2010 @ 12:03 AM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM