This project is archived and is in readonly mode.

#6047 new
Ken Mayer

[Patch] JSON.generate raises exception in v3.0.2 + json_pure => '~>1.4.3'

Reported by Ken Mayer | November 23rd, 2010 @ 06:14 PM

When using the json_pure gem, this fails in v3.0.2, but succeeds in v3.0.0. It does not fail with json 1.4.6 with native extensions.

Here's the commit the created the bug (determined by git bisect start v3.0.2 v3.0.0):

https://github.com/rails/rails/commit/33b954005cd71f1bfba1beca29680...

How to reproduce

You'll need the following Gemfile in a test rails application:

source 'http://rubygems.org'
gem 'rails', :path => '../rails/'
gem 'json_pure'

Then run this on the command line:

bundle exec rails runner 'JSON.generate([0])'

Here's a stack trace:

/Users/kmayer/Documents/rails/railties/lib/rails/commands/runner.rb:50: undefined method `merge' for #<JSON::Pure::Generator::State:0x102181890> (NoMethodError)
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:46:in `encode'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:77:in `check_for_circular_references'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:45:in `encode'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/json/encoding.rb:30:in `encode'
    from /Users/kmayer/Documents/rails/activesupport/lib/active_support/core_ext/object/to_json.rb:15:in `to_json'
    from /Users/kmayer/.rvm/gems/ruby-1.8.7-head@jsonbug/gems/json_pure-1.4.6/lib/json/pure/generator.rb:228:in `generate'
    from /Users/kmayer/.rvm/gems/ruby-1.8.7-head@jsonbug/gems/json_pure-1.4.6/lib/json/common.rb:210:in `generate'
    from (eval):1
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands.rb:39:in `eval'
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands/runner.rb:50
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands.rb:39:in `require'
    from /Users/kmayer/Documents/rails/railties/lib/rails/commands.rb:39
    from script/rails:6:in `require'
    from script/rails:6

Comments and changes to this ticket

  • Ken Mayer

    Ken Mayer December 2nd, 2010 @ 08:14 PM

    Patch file with test & fix...

  • Ken Mayer

    Ken Mayer December 2nd, 2010 @ 08:21 PM

    • no changes were found...
  • Ken Mayer

    Ken Mayer December 2nd, 2010 @ 08:21 PM

    • Tag changed from activesupport, json, rails3.0.2 to activesupport, json, patched, rails3.0.2
  • Ken Mayer
  • Ken Mayer

    Ken Mayer December 2nd, 2010 @ 08:40 PM

    
    From f52f3295196d48015c1bd5f337a5978e14c3acd6 Mon Sep 17 00:00:00 2001
    From: Kenneth Mayer <kmayer@bitwrangler.com>
    Date: Thu, 2 Dec 2010 09:11:08 -0800
    Subject: [PATCH] bug: JSON::Pure reuses a State object for options
    
    Fixes [#6047 state:resolved]
    
    An earlier commit (33b95) tries to merging encoding
    options (active_support/json/encoding.rb:#options_for)
    would raise a method_missing error -- but only
    if you were using json_pure.
    
    This converts State objects back to an options hash
    via :to_h
    ---
     activesupport/lib/active_support/json/encoding.rb |    4 ++--
     activesupport/test/json/encoding_test.rb          |    5 +++++
     2 files changed, 7 insertions(+), 2 deletions(-)
    
    diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb
    index c8cac52..e8d14b3 100644
    --- a/activesupport/lib/active_support/json/encoding.rb
    +++ b/activesupport/lib/active_support/json/encoding.rb
    @@ -36,8 +36,8 @@ module ActiveSupport
           class Encoder
             attr_reader :options
     
    -        def initialize(options = nil)
    -          @options = options
    +        def initialize(options_or_state = nil)
    +          @options = options_or_state.respond_to?(:to_h) ? options_or_state.to_h : options_or_state
               @seen = []
             end
     
    diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
    index e0494de..622eca2 100644
    --- a/activesupport/test/json/encoding_test.rb
    +++ b/activesupport/test/json/encoding_test.rb
    @@ -125,6 +125,11 @@ class TestJSONEncoding < Test::Unit::TestCase
         a[:sub] << a
         assert_raise(ActiveSupport::JSON::Encoding::CircularReferenceError) { ActiveSupport::JSON.encode(a) }
       end
    +  
    +  def test_no_exception_raised_with_json_pure
    +    require 'json/pure/generator'
    +    assert_equal "[]", JSON::Pure::Generator::State.new.generate([])
    +  end
     
       def test_hash_key_identifiers_are_always_quoted
         values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
    -- 
    1.7.2.2
    
  • Ken Mayer

    Ken Mayer December 3rd, 2010 @ 04:09 AM

    • Title changed from “JSON.generate raises exception in v3.0.2 + json_pure => '~>1.4.3'” to “[Patch] JSON.generate raises exception in v3.0.2 + json_pure => '~>1.4.3'”
  • Ken Mayer

    Ken Mayer December 3rd, 2010 @ 04:12 AM

    • Tag changed from activesupport, json, patched, rails3.0.2 to activesupport, json, patched
  • dvdplm

    dvdplm December 28th, 2010 @ 06:05 PM

    Confirmed in rails 3.0.3

  • Josh Kalderimis

    Josh Kalderimis February 11th, 2011 @ 01:49 PM

    • Tag changed from activesupport, json, patched to activesupport, json

    Hi Ken,

    I have tried replicating this issue but don't see any errors like you mention.

    I am pretty sure that this is more of a json_pure issue than a rails issue.

    For example

    bundle exec rails runner 'JSON.generate([0])'
    

    is not testing how active support helps generate json, it is only running the generate json_pure method within a rails environment.

    Also, your test is not testing rails at all but only json_pure.

    Have you tried updating your json_pure version?

    Thanks,

    Josh

  • bingbing

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