Add tests for OA3 components/schemas loader with upgrade notice

This commit is contained in:
Greg Myers 2020-03-21 20:19:51 +00:00
parent 095067792f
commit 23a1074d07
2 changed files with 63 additions and 26 deletions

View File

@ -70,7 +70,7 @@ module Rswag
swagger_doc.slice(:definitions)
else # Openapi3
if swagger_doc.has_key?(:definitions)
warn('Rswag::Specs: WARNING: definitions is replaced in OpenAPI3! Rename to components/schemas (in swagger_helper.rb)')
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: definitions is replaced in OpenAPI3! Rename to components/schemas (in swagger_helper.rb)')
return swagger_doc.slice(:definitions)
else
components = swagger_doc[:components] || {}

View File

@ -10,6 +10,7 @@ module Rswag
before do
allow(config).to receive(:get_swagger_doc).and_return(swagger_doc)
allow(config).to receive(:get_swagger_doc_version).and_return('2.0')
end
let(:config) { double('config') }
let(:swagger_doc) { {} }
@ -44,7 +45,7 @@ module Rswag
OpenStruct.new(
code: '200',
headers: { 'X-Rate-Limit-Limit' => '10' },
body: "{\"text\":\"Some comment\"}"
body: '{"text":"Some comment"}'
)
end
@ -54,43 +55,79 @@ module Rswag
context "response code differs from metadata" do
before { response.code = '400' }
it { expect { call }.to raise_error /Expected response code/ }
it { expect { call }.to raise_error(/Expected response code/) }
end
context "response headers differ from metadata" do
before { response.headers = {} }
it { expect { call }.to raise_error /Expected response header/ }
it { expect { call }.to raise_error(/Expected response header/) }
end
context "response body differs from metadata" do
before { response.body = "{\"foo\":\"Some comment\"}" }
it { expect { call }.to raise_error /Expected response body/ }
before { response.body = '{"foo":"Some comment"}' }
it { expect { call }.to raise_error(/Expected response body/) }
end
context 'referenced schemas' do
before do
swagger_doc[:definitions] = {
'blog' => {
type: :object,
properties: { foo: { type: :string } },
required: [ 'foo' ]
context 'swagger 2.0' do
before do
swagger_doc[:definitions] = {
'blog' => {
type: :object,
properties: { foo: { type: :string } },
required: [ 'foo' ]
}
}
}
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
## OA3
# swagger_doc[:components] = {}
# swagger_doc[:components][:schemas] = {
# 'blog' => {
# type: :object,
# properties: { foo: { type: :string } },
# required: ['foo']
# }
# }
# metadata[:response][:content]['application/json'][:schema] = { '$ref' => '#/components/schemas/blog' }
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
end
it 'uses the referenced schema to validate the response body' do
expect { call }.to raise_error(/Expected response body/)
end
end
it 'uses the referenced schema to validate the response body' do
expect { call }.to raise_error /Expected response body/
context 'openapi 3.0.1' do
context 'components/schemas' do
before do
allow(ActiveSupport::Deprecation).to receive(:warn)
allow(config).to receive(:get_swagger_doc_version).and_return('3.0.1')
swagger_doc[:components] = {
schemas: {
'blog' => {
type: :object,
properties: { foo: { type: :string } },
required: [ 'foo' ]
}
}
}
metadata[:response][:schema] = { '$ref' => '#/components/schemas/blog' }
end
it 'uses the referenced schema to validate the response body' do
expect { call }.to raise_error(/Expected response body/)
end
end
context 'deprecated definitions' do
before do
allow(ActiveSupport::Deprecation).to receive(:warn)
allow(config).to receive(:get_swagger_doc_version).and_return('3.0.1')
swagger_doc[:definitions] = {
'blog' => {
type: :object,
properties: { foo: { type: :string } },
required: [ 'foo' ]
}
}
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
end
it 'warns the user to upgrade' do
expect { call }.to raise_error(/Expected response body/)
expect(ActiveSupport::Deprecation).to have_received(:warn)
.with('Rswag::Specs: WARNING: definitions is replaced in OpenAPI3! Rename to components/schemas (in swagger_helper.rb)')
end
end
end
end
end