mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Add tests for OA3 components/schemas loader with upgrade notice
This commit is contained in:
parent
095067792f
commit
23a1074d07
@ -70,7 +70,7 @@ module Rswag
|
|||||||
swagger_doc.slice(:definitions)
|
swagger_doc.slice(:definitions)
|
||||||
else # Openapi3
|
else # Openapi3
|
||||||
if swagger_doc.has_key?(:definitions)
|
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)
|
return swagger_doc.slice(:definitions)
|
||||||
else
|
else
|
||||||
components = swagger_doc[:components] || {}
|
components = swagger_doc[:components] || {}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ module Rswag
|
|||||||
|
|
||||||
before do
|
before do
|
||||||
allow(config).to receive(:get_swagger_doc).and_return(swagger_doc)
|
allow(config).to receive(:get_swagger_doc).and_return(swagger_doc)
|
||||||
|
allow(config).to receive(:get_swagger_doc_version).and_return('2.0')
|
||||||
end
|
end
|
||||||
let(:config) { double('config') }
|
let(:config) { double('config') }
|
||||||
let(:swagger_doc) { {} }
|
let(:swagger_doc) { {} }
|
||||||
@ -44,7 +45,7 @@ module Rswag
|
|||||||
OpenStruct.new(
|
OpenStruct.new(
|
||||||
code: '200',
|
code: '200',
|
||||||
headers: { 'X-Rate-Limit-Limit' => '10' },
|
headers: { 'X-Rate-Limit-Limit' => '10' },
|
||||||
body: "{\"text\":\"Some comment\"}"
|
body: '{"text":"Some comment"}'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -54,20 +55,21 @@ module Rswag
|
|||||||
|
|
||||||
context "response code differs from metadata" do
|
context "response code differs from metadata" do
|
||||||
before { response.code = '400' }
|
before { response.code = '400' }
|
||||||
it { expect { call }.to raise_error /Expected response code/ }
|
it { expect { call }.to raise_error(/Expected response code/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "response headers differ from metadata" do
|
context "response headers differ from metadata" do
|
||||||
before { response.headers = {} }
|
before { response.headers = {} }
|
||||||
it { expect { call }.to raise_error /Expected response header/ }
|
it { expect { call }.to raise_error(/Expected response header/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "response body differs from metadata" do
|
context "response body differs from metadata" do
|
||||||
before { response.body = "{\"foo\":\"Some comment\"}" }
|
before { response.body = '{"foo":"Some comment"}' }
|
||||||
it { expect { call }.to raise_error /Expected response body/ }
|
it { expect { call }.to raise_error(/Expected response body/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'referenced schemas' do
|
context 'referenced schemas' do
|
||||||
|
context 'swagger 2.0' do
|
||||||
before do
|
before do
|
||||||
swagger_doc[:definitions] = {
|
swagger_doc[:definitions] = {
|
||||||
'blog' => {
|
'blog' => {
|
||||||
@ -77,20 +79,55 @@ module Rswag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
|
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' }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'uses the referenced schema to validate the response body' do
|
it 'uses the referenced schema to validate the response body' do
|
||||||
expect { call }.to raise_error /Expected response body/
|
expect { call }.to raise_error(/Expected response body/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user