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

Oj compatibility with mimic json mode #2254

Merged
merged 1 commit into from Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion gems/aws-sdk-core/CHANGELOG.md
@@ -1,11 +1,12 @@
Unreleased Changes
------------------

* Issue - Rescue from `JSON::ParserError` when using `Oj.mimic_JSON`. (#2247)

3.91.0 (2020-03-09)
------------------

* Feature - Updated Aws::STS::Client with the latest API changes.

* Feature - Add `standard` and `adaptive` retry modes.

3.90.1 (2020-02-14)
Expand Down
19 changes: 9 additions & 10 deletions gems/aws-sdk-core/lib/aws-sdk-core/json.rb
Expand Up @@ -7,28 +7,24 @@
module Aws
# @api private
module Json

class ParseError < StandardError

def initialize(error)
@error = error
super(error.message)
end

attr_reader :error

end

class << self

def load(json)
ENGINE.load(json, *ENGINE_LOAD_OPTIONS)
rescue *ENGINE_ERRORS => e
raise ParseError.new(e)
raise ParseError, e
end

def load_file(path)
self.load(File.open(path, 'r', encoding: 'UTF-8') { |f| f.read })
load(File.open(path, 'r', encoding: 'UTF-8', &:read))
end

def dump(value)
Expand All @@ -39,7 +35,12 @@ def dump(value)

def oj_engine
require 'oj'
[Oj, [{mode: :compat, symbol_keys: false}], [{ mode: :compat }], oj_parse_error]
[
Oj,
[{ mode: :compat, symbol_keys: false }],
[{ mode: :compat }],
oj_parse_error
]
rescue LoadError
false
end
Expand All @@ -50,17 +51,15 @@ def json_engine

def oj_parse_error
if Oj.const_defined?('ParseError')
[Oj::ParseError, EncodingError]
[Oj::ParseError, EncodingError, JSON::ParserError]
else
[SyntaxError]
end
end

end

# @api private
ENGINE, ENGINE_LOAD_OPTIONS, ENGINE_DUMP_OPTIONS, ENGINE_ERRORS =
oj_engine || json_engine

end
end
26 changes: 12 additions & 14 deletions gems/aws-sdk-core/spec/aws/json_spec.rb
Expand Up @@ -2,54 +2,52 @@

module Aws
describe Json do

describe '.load' do
subject(:load) { described_class.load(raw_json) }

shared_examples 'loads JSON correctly' do
let(:raw_json) {'{ "foo": "bar" }'}
let(:raw_json) { '{ "foo": "bar" }' }

it 'returns an empty hash' do
expect(subject).to eq({ 'foo' => 'bar' })
it 'returns a hash with the JSON' do
expect(subject).to eq('foo' => 'bar')
end

context 'not JSON' do
# OJ gem raises EncodingError in this case
let(:raw_json) {'<ServiceUnavailableException/>'}
# OJ can also raise JSON::ParserError if using Oj.mimic_JSON
let(:raw_json) { '<ServiceUnavailableException/>' }

it 'raises a ParseError' do
expect { subject }.to raise_error(Aws::Json::ParseError)
end
end

context 'invalid JSON' do
let(:raw_json) {'{ "steve": }'}
let(:raw_json) { '{ "steve": }' }

it 'raises a ParseError' do
expect { subject }.to raise_error(Aws::Json::ParseError)
end
end

end

context 'when using oj gem' do
include_examples 'loads JSON correctly'
end

context 'when using bundled json' do

before do
engine, load_options, dump_options, errors = described_class.send(:json_engine)
engine, load_options, dump_options, errors =
described_class.send(:json_engine)

stub_const("Aws::Json::ENGINE", engine)
stub_const("Aws::Json::ENGINE_LOAD_OPTIONS", load_options)
stub_const("Aws::Json::ENGINE_DUMP_OPTIONS", dump_options)
stub_const("Aws::Json::ENGINE_ERRORS", errors)
stub_const('Aws::Json::ENGINE', engine)
stub_const('Aws::Json::ENGINE_LOAD_OPTIONS', load_options)
stub_const('Aws::Json::ENGINE_DUMP_OPTIONS', dump_options)
stub_const('Aws::Json::ENGINE_ERRORS', errors)
end

include_examples 'loads JSON correctly'
end
end

end
end