From 23a1074d07b2b6c44309fc4f5fd514d7926da9a0 Mon Sep 17 00:00:00 2001 From: Greg Myers Date: Sat, 21 Mar 2020 20:19:51 +0000 Subject: [PATCH] Add tests for OA3 components/schemas loader with upgrade notice --- .../lib/rswag/specs/response_validator.rb | 2 +- .../rswag/specs/response_validator_spec.rb | 87 +++++++++++++------ 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/rswag-specs/lib/rswag/specs/response_validator.rb b/rswag-specs/lib/rswag/specs/response_validator.rb index 3d97562..ccba97c 100644 --- a/rswag-specs/lib/rswag/specs/response_validator.rb +++ b/rswag-specs/lib/rswag/specs/response_validator.rb @@ -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] || {} diff --git a/rswag-specs/spec/rswag/specs/response_validator_spec.rb b/rswag-specs/spec/rswag/specs/response_validator_spec.rb index 5eeabde..24882fe 100644 --- a/rswag-specs/spec/rswag/specs/response_validator_spec.rb +++ b/rswag-specs/spec/rswag/specs/response_validator_spec.rb @@ -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