ohler55 / oj

Optimized JSON
http://www.ohler.com/oj
MIT License
3.12k stars 250 forks source link

Oj::Rails.mimic_JSON() raises Oj::ParseError invalid float #890

Closed senid231 closed 11 months ago

senid231 commented 11 months ago

After call Oj.mimic_JSON or Oj::Rails.mimic_JSON
JSON.parseto raises Oj::ParseError (instead of JSON::ParserError) when json string contains token started with 1+ digits and "e" letter but is not a valid float (for ex. 84e crash, but 84e-1 is not)

If I comment out Oj.mimic_JSON and Oj::Rails.mimic_JSON than below tests pass.

reproduce

# frozen_string_literal: true

begin
  require 'bundler/inline'
rescue LoadError => e
  warn 'Bundler version 1.10 or later is required. Please update your Bundler'
  raise e
end

puts "Ruby #{RUBY_VERSION}"
gemfile(true) do
  source 'https://rubygems.org'

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem 'activesupport'
  gem 'oj', '3.15.0'
end

require 'active_support'
require 'json'
require 'oj'

Oj.mimic_JSON()
# Oj::Rails.mimic_JSON()

require 'minitest/autorun'

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

class BugTest < Minitest::Test
  def test_invalid_float_short
    error = assert_raises(::JSON::ParserError) do
      ::JSON.parse('{ "foo": 84e }')
    end
    assert_equal "unexpected token at '{ \"foo\": 84e }'", error.message
  end

  def test_invalid_float_long
    error = assert_raises(::JSON::ParserError) do
      ::JSON.parse('{ "foo": 84eb234 }')
    end
    assert_equal "unexpected token at '{ \"foo\": 84eb234 }'", error.message
  end
end

log

Ruby 3.2.2
Fetching gem metadata from https://rubygems.org/........
Resolving dependencies...
Using concurrent-ruby 1.2.2
Using minitest 5.18.1
Using oj 3.15.0
Using bundler 2.4.12
Using tzinfo 2.0.6
Using i18n 1.14.1
Using activesupport 7.0.6

Minitest::Assertion: [JSON::ParserError] exception expected, not
Class: <Oj::ParseError>
Message: <"Invalid float">
---Backtrace---
/home/senid/projects/bug_reports/oj/test_oj_parse_error_expose_bug.rb:42:in `parse'
/home/senid/projects/bug_reports/oj/test_oj_parse_error_expose_bug.rb:42:in `block in test_invalid_float_long'
---------------
/home/senid/projects/bug_reports/oj/test_oj_parse_error_expose_bug.rb:41:in `test_invalid_float_long'

Minitest::Assertion: [JSON::ParserError] exception expected, not
Class: <Oj::ParseError>
Message: <"Invalid float">
---Backtrace---
/home/senid/projects/bug_reports/oj/test_oj_parse_error_expose_bug.rb:35:in `parse'
/home/senid/projects/bug_reports/oj/test_oj_parse_error_expose_bug.rb:35:in `block in test_invalid_float_short'
---------------
/home/senid/projects/bug_reports/oj/test_oj_parse_error_expose_bug.rb:34:in `test_invalid_float_short'

Process finished with exit code 1
senid231 commented 11 months ago

looks like similar to #555 and #663 but not a duplicate

ohler55 commented 11 months ago

I'll see what I can do to make the exceptions more compatible.

ohler55 commented 11 months ago

Please try the compat-errors branch.

ohler55 commented 11 months ago

Release v3.15.1 includes the fix.