From b21c76ba75ee40d4137217669d7e124f1061c566 Mon Sep 17 00:00:00 2001 From: Matt Muller Date: Mon, 9 Mar 2020 12:07:50 -0700 Subject: [PATCH] Oj compatibility with mimic json mode --- gems/aws-sdk-core/CHANGELOG.md | 3 ++- gems/aws-sdk-core/lib/aws-sdk-core/json.rb | 19 ++++++++-------- gems/aws-sdk-core/spec/aws/json_spec.rb | 26 ++++++++++------------ 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/gems/aws-sdk-core/CHANGELOG.md b/gems/aws-sdk-core/CHANGELOG.md index 9db2da1e1fd..739073dd145 100644 --- a/gems/aws-sdk-core/CHANGELOG.md +++ b/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) diff --git a/gems/aws-sdk-core/lib/aws-sdk-core/json.rb b/gems/aws-sdk-core/lib/aws-sdk-core/json.rb index e66b1ba542f..111fa2fe2dd 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core/json.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core/json.rb @@ -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) @@ -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 @@ -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 diff --git a/gems/aws-sdk-core/spec/aws/json_spec.rb b/gems/aws-sdk-core/spec/aws/json_spec.rb index 0df092fd1de..7073d930f29 100644 --- a/gems/aws-sdk-core/spec/aws/json_spec.rb +++ b/gems/aws-sdk-core/spec/aws/json_spec.rb @@ -2,20 +2,20 @@ 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) {''} + # OJ can also raise JSON::ParserError if using Oj.mimic_JSON + let(:raw_json) { '' } it 'raises a ParseError' do expect { subject }.to raise_error(Aws::Json::ParseError) @@ -23,13 +23,12 @@ module Aws 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 @@ -37,19 +36,18 @@ module Aws 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