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.

ActiveSupport::JSON.encode bogus CircularReferenceError

#3744

in encoding.rb line 68:

raise CircularReferenceError, 'object references itself' if seen.include?(value)

I think this should read something like:

raise CircularReferenceError, 'object references itself' if seen.detect { |i| value.equal?(i) }

i.e. it should test for equality at the object level, rather than via the ==() method (which is occassionally overidden, and can erroneously trigger the CircularReferenceError).

Reported by Andrew Williams · January 19th, 2010 @ 03:40 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: Low

Activity

  1. Sean Wolfe
    Sean Wolfe

    I can verify this problem as well. Having 2 datetime types in the json output causes this failure as well.
    I used the described patch above to get around this problem.

    March 2nd, 2010 @ 11:07 PM

  2. testman
    testman

    I can verify this as well. This patch suppressed the error at least.

    April 5th, 2010 @ 12:11 AM

  3. Jeremy Kemper
    Jeremy Kemper

    Patch and test welcome!

    April 5th, 2010 @ 03:04 AM

  4. Jeremy Kemper
    Jeremy Kemper
    • State changed from new to open
    • Assigned user set to Jeremy Kemper

    April 5th, 2010 @ 03:05 AM

  5. Lawrence Pit
    Lawrence Pit

    This can be closed, the line currently reads (as suggested above as patch) :

       if @seen.any? { |object| object.equal?(value) }
    

    Edited by Rohit Arondekar for formating.

    May 11th, 2010 @ 11:56 AM

  6. David Trasbo
    David Trasbo
    • Assigned user changed from Jeremy Kemper to Rohit Arondekar
    • Importance changed from to Low

    Can be closed. See above.

    July 1st, 2010 @ 08:31 AM

  7. Rohit Arondekar
    Rohit Arondekar
    • State changed from open to resolved

    July 1st, 2010 @ 08:33 AM

  8. Josh Delsman
    Josh Delsman
    • Tag changed from json activesupport circularreferenceerror to activesupport, circularreferenceerror, json

    This hasn't been fixed. I still see an issue here:

    ActiveSupport::JSON::Encoding::CircularReferenceError: object references itself
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:57:in `check_for_circular_references'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:45:in `encode'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:209:in `block in encode_json'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:209:in `each'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:209:in `map'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:209:in `encode_json'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:46:in `block in encode'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:60:in `check_for_circular_references'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:45:in `encode'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/json/encoding.rb:30:in `encode'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/activesupport/lib/active_support/core_ext/object/to_json.rb:20:in `to_json'
    from (irb):10
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/railties/lib/rails/commands/console.rb:44:in `start'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/railties/lib/rails/commands/console.rb:8:in `start'
    from /Users/jdelsman/.rvm/gems/ruby-1.9.2-rc2/bundler/gems/rails-b378b19/railties/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
    

    July 23rd, 2010 @ 06:10 AM

  9. Will Prater
    Will Prater

    Yes, this does not appear to be fixed in Rails3 beta4. I have a date field and am trying to use to_json with no success.


    ruby-1.8.7-p249 > PayPeriod
    => PayPeriod(id: integer, start_date: date, end_date: date, pay_date: date)

    ruby-1.8.7-p249 > PayPeriod.limit(1) => [#]

    ruby-1.8.7-p249 > PayPeriod.limit(1).to_json
    ActiveSupport::JSON::Encoding::CircularReferenceError: object references itself

    {mkd-extraction-bd47fe22ed55c8d20a7d00c63404312e}

    Any ideas for a temporary fix?

    August 1st, 2010 @ 11:21 PM

  10. Throlkim
    Throlkim

    Getting this error still in Rails 3.0.0:

    ActiveSupport::JSON::Encoding::CircularReferenceError in SearchesController#new
    object references itself
    
    activesupport (3.0.0) lib/active_support/json/encoding.rb:57:in `check_for_circular_references'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:45:in `encode'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:209:in `block in encode_json'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:209:in `each'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:209:in `map'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:209:in `encode_json'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:46:in `block in encode'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:60:in `check_for_circular_references'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:45:in `encode'
    activesupport (3.0.0) lib/active_support/json/encoding.rb:30:in `encode'
    actionpack (3.0.0) lib/action_controller/metal/renderers.rb:74:in `block in <module:Renderers>'
    actionpack (3.0.0) lib/action_controller/metal/renderers.rb:32:in `_handle_render_options'
    actionpack (3.0.0) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
    actionpack (3.0.0) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
    actionpack (3.0.0) lib/abstract_controller/rendering.rb:101:in `render_to_string'
    actionpack (3.0.0) lib/abstract_controller/rendering.rb:92:in `render'
    actionpack (3.0.0) lib/action_controller/metal/rendering.rb:17:in `render'
    actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
    activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
    /home/throlkim/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
    activesupport (3.0.0) lib/active_support/core_ext/benchmark.rb:5:in `ms'
    actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
    actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
    activerecord (3.0.0) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
    actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:39:in `render'
    actionpack (3.0.0) lib/action_controller/metal/responder.rb:209:in `display'
    actionpack (3.0.0) lib/action_controller/metal/responder.rb:159:in `api_behavior'
    actionpack (3.0.0) lib/action_controller/metal/responder.rb:138:in `rescue in to_format'
    actionpack (3.0.0) lib/action_controller/metal/responder.rb:136:in `to_format'
    actionpack (3.0.0) lib/action_controller/metal/responder.rb:119:in `respond'
    actionpack (3.0.0) lib/action_controller/metal/responder.rb:112:in `call'
    actionpack (3.0.0) lib/action_controller/metal/mime_responds.rb:232:in `respond_with'
    actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
    actionpack (3.0.0) lib/abstract_controller/base.rb:150:in `process_action'
    actionpack (3.0.0) lib/action_controller/metal/rendering.rb:11:in `process_action'
    actionpack (3.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
    activesupport (3.0.0) lib/active_support/callbacks.rb:435:in `_run__29503673__process_action__515564314__callbacks'
    activesupport (3.0.0) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
    activesupport (3.0.0) lib/active_support/callbacks.rb:93:in `run_callbacks'
    actionpack (3.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
    actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
    activesupport (3.0.0) lib/active_support/notifications.rb:52:in `block in instrument'
    activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument'
    actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
    actionpack (3.0.0) lib/action_controller/metal/rescue.rb:17:in `process_action'
    actionpack (3.0.0) lib/abstract_controller/base.rb:119:in `process'
    actionpack (3.0.0) lib/abstract_controller/rendering.rb:40:in `process'
    actionpack (3.0.0) lib/action_controller/metal.rb:133:in `dispatch'
    actionpack (3.0.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
    actionpack (3.0.0) lib/action_controller/metal.rb:173:in `block in action'
    actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in `call'
    actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
    actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:27:in `call'
    rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call'
    rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize'
    rack-mount (0.6.13) lib/rack/mount/code_generation.rb:75:in `optimized_each'
    rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize'
    rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call'
    actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:492:in `call'
    haml (3.0.21) lib/sass/plugin/rack.rb:41:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/head.rb:14:in `call'
    rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/flash.rb:182:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/cookies.rb:287:in `call'
    activerecord (3.0.0) lib/active_record/query_cache.rb:32:in `block in call'
    activerecord (3.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
    activerecord (3.0.0) lib/active_record/query_cache.rb:12:in `cache'
    activerecord (3.0.0) lib/active_record/query_cache.rb:31:in `call'
    activerecord (3.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
    activesupport (3.0.0) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
    actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
    rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
    railties (3.0.0) lib/rails/rack/logger.rb:13:in `call'
    rack (1.2.1) lib/rack/runtime.rb:17:in `call'
    activesupport (3.0.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
    rack (1.2.1) lib/rack/lock.rb:11:in `block in call'
    <internal:prelude>:10:in `synchronize'
    rack (1.2.1) lib/rack/lock.rb:11:in `call'
    actionpack (3.0.0) lib/action_dispatch/middleware/static.rb:30:in `call'
    railties (3.0.0) lib/rails/application.rb:168:in `call'
    railties (3.0.0) lib/rails/application.rb:77:in `method_missing'
    railties (3.0.0) lib/rails/rack/log_tailer.rb:14:in `call'
    rack (1.2.1) lib/rack/content_length.rb:13:in `call'
    rack (1.2.1) lib/rack/chunked.rb:15:in `call'
    rack (1.2.1) lib/rack/handler/mongrel.rb:67:in `process'
    mongrel (1.2.0.pre2) lib/mongrel.rb:165:in `block in process_client'
    mongrel (1.2.0.pre2) lib/mongrel.rb:164:in `each'
    mongrel (1.2.0.pre2) lib/mongrel.rb:164:in `process_client'
    mongrel (1.2.0.pre2) lib/mongrel.rb:291:in `block (2 levels) in run'
    

    October 14th, 2010 @ 10:40 AM

  11. Rohit Arondekar
    Rohit Arondekar
    • State changed from resolved to open
    • Milestone cleared.

    Could one of you please write a patch with tests. Contributor guide can be found here — http://rails.lighthouseapp.com/projects/8994/sending-patches

    Also I apologize for not taking a look at this earlier.

    October 14th, 2010 @ 10:48 AM

  12. Jeremy Kemper
  13. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:33 AM

  14. Neeraj Singh
    Neeraj Singh
    • State changed from open to needs-more-info
    • Milestone changed from 3.0.2 to 3.x

    @Will Prater: I am not able to reproduce it with rails edge. Can you show me your model, migration and the data to reproduce this problem.

    ree-1.8.7-2010.02 > Car.first.to_json
      Car Load (0.2ms)  SELECT "cars".* FROM "cars" LIMIT 1
     => "{\"car\":{\"name\":\"honda\",\"manufactured_at\":\"2010-11-15T21:05:00Z\",\"color\":null,\"id\":1}}"
    

    November 15th, 2010 @ 09:06 PM

  15. Matt Todd
    Matt Todd

    I am only able to reproduce the issue with a ThinkingSphinx::Search object, not an actual array of results.

    One easy test is to try:

    results.to_a.to_json
    

    This was sufficient enough for me to indicate that it was an issue with the library and not with Rails.

    Please try this to make sure you're dealing with an array of results or with a lazy loaded object that doesn't handle to_json correctly.

    November 18th, 2010 @ 08:03 PM

  16. Rohit Arondekar
    Rohit Arondekar
    • Assigned user cleared.

    Can we close this ticket or is still unresolved?

    January 26th, 2011 @ 10:03 AM

  17. Alexey Nayden
    Alexey Nayden

    Current rails 3.0.3 code has the following implementation:

    if @seen.any? { |object| object.equal?(value) }
      raise CircularReferenceError, 'object references itself'
    end
    

    So the ticket should be fine now, the problem with "==" has been solved

    January 29th, 2011 @ 07:53 PM

  18. rails
    rails
    • State changed from needs-more-info to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    April 30th, 2011 @ 01:00 AM

  19. rails
    rails
    • State changed from open to stale

    April 30th, 2011 @ 01:00 AM