Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

active_support/json doesn't handle IO objects correctly #26132

Closed
liudangyi opened this issue Aug 12, 2016 · 12 comments
Closed

active_support/json doesn't handle IO objects correctly #26132

liudangyi opened this issue Aug 12, 2016 · 12 comments

Comments

@liudangyi
Copy link

Steps to reproduce

irb(main):001:0> require 'json'
=> true
irb(main):002:0> STDOUT.to_json
=> "\"#<IO:0x007fcb1189ebd8>\""
irb(main):003:0> require 'active_support/json'
=> true
irb(main):004:0> STDOUT.to_json
IOError: not opened for reading
    from /Users/Leedy/Developer/github/rails/activesupport/lib/active_support/core_ext/object/json.rb:132:in `each'
    from /Users/Leedy/Developer/github/rails/activesupport/lib/active_support/core_ext/object/json.rb:132:in `to_a'
    from /Users/Leedy/Developer/github/rails/activesupport/lib/active_support/core_ext/object/json.rb:132:in `as_json'
    from /Users/Leedy/Developer/github/rails/activesupport/lib/active_support/json/encoding.rb:33:in `encode'
    from /Users/Leedy/Developer/github/rails/activesupport/lib/active_support/json/encoding.rb:20:in `encode'
    from /Users/Leedy/Developer/github/rails/activesupport/lib/active_support/core_ext/object/json.rb:39:in `to_json'
    from (irb):4
    from /usr/local/bin/irb:11:in `<main>'

Expected behavior

to_json should never fail.

Actual behavior

It raises an error because STDOUT.to_a fails.

# activesupport/lib/active_support/core_ext/object/json.rb
module Enumerable
  def as_json(options = nil) #:nodoc:
    to_a.as_json(options)
  end
end

System configuration

Rails version: master (5.1.0.alpha)

Ruby version: 2.3.1p112

@veekram
Copy link

veekram commented Aug 12, 2016

@liudangyi I am not sure but may it has something to do with this

@liudangyi
Copy link
Author

@veekram Yes, I understand why it happened. But I think it's still a bug of activesupport, since to_json should be expected to never fail.

liudangyi added a commit to liudangyi/jquery-ujs that referenced this issue Aug 12, 2016
Introducing activesupport will break env.to_json, so use JSON.generate
instead. See rails/rails#26132
@sgrif
Copy link
Contributor

sgrif commented Aug 14, 2016

since to_json should be expected to never fail.

Where are you getting that idea from?

@liudangyi
Copy link
Author

I was trying to serialize request.env in sinatra and found it'a OK when only requiring "json" but bugs when requiring "active_support".

So introducing "active_support" just breaks the existing code, right?

@stale
Copy link

stale bot commented Mar 27, 2017

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 team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@stale stale bot added the stale label Mar 27, 2017
@betesh
Copy link
Contributor

betesh commented Mar 27, 2017

This can still be reproduced on master. Adding this to activesupport/test/json/encoding_test.rb demonstrates it:

class Loggable
  def initialize
    @io = STDOUT
  end
end

def test_to_json_works_on_a_logger
  assert_nothing_raised { Loggable.new.to_json }
end

@rails-bot
Copy link

rails-bot bot commented Jun 26, 2017

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 team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@betesh
Copy link
Contributor

betesh commented Jun 27, 2017

This still fails on both 5-1-stable (198bc1f) and master (61cc630)

@rails-bot
Copy link

rails-bot bot commented Sep 25, 2017

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 team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@betesh
Copy link
Contributor

betesh commented Sep 25, 2017

This still fails on both 5-1-stable (581fd57) and master (70656c4)

@rails-bot rails-bot bot removed the stale label Sep 25, 2017
rohitpaulk added a commit to rohitpaulk/rails that referenced this issue Oct 23, 2017
rafaelfranca added a commit that referenced this issue Oct 23, 2017
@rhuanbarreto
Copy link

You should fix this also for rails 5.0

rhuanbarreto added a commit to rhuanbarreto/rails that referenced this issue Feb 26, 2018
There is some incompatibility generating the following error when you use the gems JSON and ActiveSupport:
IOError (not opened for reading).
This change fixes it.
@inertia186
Copy link

I just observed this in activesupport-5.1.6. I have a script that does not require activesupport, but it's in my Gemfile for other scripts. Temporarily removing activesupport from my Gemfile fixes the issue. Problem appears to have been fixed in activesupport-5.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants