Skip to content

Commit 75e57d9

Browse files
authored
Merge pull request #379 from ahx/fix-multi-json-parse
Fix OpenapiFirst.load when MultiJson is configured to return symbol keys
2 parents c727a15 + a76a5aa commit 75e57d9

File tree

9 files changed

+45
-6
lines changed

9 files changed

+45
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix OpenapiFirst.load when MultiJson is configured to return symbol keys
6+
57
## 2.9.2
68

79
- OpenapiFirst::Test reports all non-covered requests now

Gemfile.multi_json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ gem 'multi_json'
99
group :test, :development do
1010
gem 'actionpack'
1111
gem 'bundler'
12+
gem 'minitest'
1213
gem 'rack-test'
14+
gem 'rails'
1315
gem 'rake'
1416
gem 'rspec'
1517
gem 'rubocop'
1618
gem 'rubocop-performance'
1719
gem 'simplecov'
20+
gem 'sinatra'
1821
end

Gemfile.rack2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ gem 'rack', '< 3.0.0'
99
group :test, :development do
1010
gem 'actionpack'
1111
gem 'bundler'
12+
gem 'minitest'
1213
gem 'rack-test'
14+
gem 'rails'
1315
gem 'rake'
1416
gem 'rspec'
1517
gem 'rubocop'
18+
gem 'rubocop-performance'
1619
gem 'simplecov'
20+
gem 'sinatra'
1721
end

Gemfile.rack30

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ gem 'rack', '>= 3.0.0', '< 3.1.0'
99
group :test, :development do
1010
gem 'actionpack'
1111
gem 'bundler'
12+
gem 'minitest'
1213
gem 'rack-test'
14+
gem 'rails'
1315
gem 'rake'
1416
gem 'rspec'
1517
gem 'rubocop'
1618
gem 'rubocop-performance'
1719
gem 'simplecov'
20+
gem 'sinatra'
1821
end

Gemfile.rails6

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ gemspec
77
gem 'rackup'
88

99
group :test, :development do
10-
gem 'actionpack', '~> 6.1'
1110
gem 'bundler'
11+
gem 'minitest'
1212
gem 'rack-test'
13+
gem 'rails', '~> 6.1'
1314
gem 'rake'
1415
gem 'rspec'
1516
gem 'rubocop'
17+
gem 'rubocop-performance'
1618
gem 'simplecov'
19+
gem 'sinatra'
1720
end

lib/openapi_first.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def self.load(filepath_or_definition, only: nil, &)
6666
# @return [Definition]
6767
# TODO: This needs to work with unresolved contents as well
6868
def self.parse(contents, only: nil, filepath: nil, &)
69-
contents = JSON.parse(JSON.generate(contents)) # Deeply stringify keys, because of YAML. See https://github.com/ahx/openapi_first/issues/367
69+
contents = ::JSON.parse(::JSON.generate(contents)) # Deeply stringify keys, because of YAML. See https://github.com/ahx/openapi_first/issues/367
7070
contents['paths'].filter!(&->(key, _) { only.call(key) }) if only
7171
Definition.new(contents, filepath, &)
7272
end

lib/openapi_first/file_loader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def load(file_path)
1313

1414
body = File.read(file_path)
1515
extname = File.extname(file_path)
16-
return JSON.parse(body) if extname == '.json'
16+
return ::JSON.parse(body) if extname == '.json'
1717
return YAML.unsafe_load(body) if ['.yaml', '.yml'].include?(extname)
1818

1919
body

spec/file_loader_spec.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
RSpec.describe OpenapiFirst::FileLoader do
44
describe '.load' do
5-
it 'loads .json' do
6-
contents = described_class.load('./spec/data/petstore.json')
7-
expect(contents['openapi']).to eq('3.0.0')
5+
begin
6+
require 'multi_json'
7+
before do
8+
MultiJson.load_options = { symbolize_keys: true }
9+
end
10+
11+
after do
12+
MultiJson.load_options = { symbolize_keys: false }
13+
end
14+
rescue LoadError # rubocop:disable Lint/SuppressedException
815
end
916

1017
it 'loads .yaml' do
@@ -17,6 +24,11 @@
1724
expect(contents['openapi']).to eq('3.0.0')
1825
end
1926

27+
it 'loads .json' do
28+
contents = described_class.load('./spec/data/petstore.json')
29+
expect(contents['openapi']).to eq('3.0.0')
30+
end
31+
2032
it 'raises FileNotFoundError if file was not found' do
2133
expect { described_class.load('./spec/data/unknown.yaml') }.to raise_error(OpenapiFirst::FileNotFoundError, 'File not found "./spec/data/unknown.yaml"')
2234
end

spec/openapi_first_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838
end
3939

4040
describe '.load' do
41+
begin
42+
require 'multi_json'
43+
before do
44+
MultiJson.load_options = { symbolize_keys: true }
45+
end
46+
47+
after do
48+
MultiJson.load_options = { symbolize_keys: false }
49+
end
50+
rescue LoadError # rubocop:disable Lint/SuppressedException
51+
end
52+
4153
it 'returns a Definition' do
4254
expect(OpenapiFirst.load(spec_path)).to be_a OpenapiFirst::Definition
4355
end

0 commit comments

Comments
 (0)