From eb58fe687a8f498c3f350201a295657b9b256709 Mon Sep 17 00:00:00 2001 From: Greg Myers Date: Sun, 22 Mar 2020 00:04:11 +0000 Subject: [PATCH] add upgrade for basepath and host to server urls --- .../lib/rswag/specs/swagger_formatter.rb | 15 +++++++ .../rswag/specs/swagger_formatter_spec.rb | 15 +++++-- test-app/swagger/v1/swagger.json | 42 ++++++++++++++----- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/rswag-specs/lib/rswag/specs/swagger_formatter.rb b/rswag-specs/lib/rswag/specs/swagger_formatter.rb index 6aeeb55..ea072fe 100644 --- a/rswag-specs/lib/rswag/specs/swagger_formatter.rb +++ b/rswag-specs/lib/rswag/specs/swagger_formatter.rb @@ -36,6 +36,7 @@ module Rswag if !doc_version(swagger_doc).start_with?('2') upgrade_request_type!(metadata) + upgrade_servers!(swagger_doc) end swagger_doc.deep_merge!(metadata_to_swagger(metadata)) @@ -136,6 +137,20 @@ module Rswag end end end + + def upgrade_servers!(swagger_doc) + if swagger_doc[:servers].nil? && swagger_doc.has_key?(:schemes) + + swagger_doc[:servers] = { urls: [] } + swagger_doc[:schemes].each do |scheme| + swagger_doc[:servers][:urls] << scheme + '://' + swagger_doc[:host] + swagger_doc[:basePath] + end + + swagger_doc.delete(:schemes) + swagger_doc.delete(:host) + swagger_doc.delete(:basePath) + end + end end end end diff --git a/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb b/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb index 33b9344..678462f 100644 --- a/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb +++ b/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb @@ -22,7 +22,6 @@ module Rswag allow(config).to receive(:get_swagger_doc).and_return(swagger_doc) subject.example_group_finished(notification) end - let(:swagger_doc) { { swagger: '2.0' } } let(:notification) { OpenStruct.new(group: OpenStruct.new(metadata: api_metadata)) } let(:api_metadata) do { @@ -34,6 +33,7 @@ module Rswag end context 'with the document tag set to false' do + let(:swagger_doc) { { swagger: '2.0' } } let(:document) { false } it 'does not update the swagger doc' do @@ -42,6 +42,7 @@ module Rswag end context 'with the document tag set to anything but false' do + let(:swagger_doc) { { swagger: '2.0' } } # anything works, including its absence when specifying responses. let(:document) { nil } @@ -67,13 +68,21 @@ module Rswag end end - context 'upgrades to 3.0' do - let(:swagger_doc) { { openapi: '3.0.1'} } + context 'with metadata upgrades for 3.0' do + let(:swagger_doc) { { + openapi: '3.0.1', + basePath: '/foo', + schemes: ['http', 'https'], + host: 'api.example.com' + } } let(:document) { nil } it 'converts query and path params, type: to schema: { type: }' do expect(swagger_doc).to match( openapi: '3.0.1', + servers: { + urls: ['http://api.example.com/foo', 'https://api.example.com/foo'] + }, paths: { '/blogs' => { parameters: [{ schema: { type: :string } }], diff --git a/test-app/swagger/v1/swagger.json b/test-app/swagger/v1/swagger.json index fd380a8..e478605 100644 --- a/test-app/swagger/v1/swagger.json +++ b/test-app/swagger/v1/swagger.json @@ -21,10 +21,14 @@ ], "responses": { "204": { - "description": "Valid credentials" + "description": "Valid credentials", + "headers": { + } }, "401": { - "description": "Invalid credentials" + "description": "Invalid credentials", + "headers": { + } } } } @@ -45,10 +49,14 @@ ], "responses": { "204": { - "description": "Valid credentials" + "description": "Valid credentials", + "headers": { + } }, "401": { - "description": "Invalid credentials" + "description": "Invalid credentials", + "headers": { + } } } } @@ -72,10 +80,14 @@ ], "responses": { "204": { - "description": "Valid credentials" + "description": "Valid credentials", + "headers": { + } }, "401": { - "description": "Invalid credentials" + "description": "Invalid credentials", + "headers": { + } } } } @@ -105,12 +117,16 @@ ], "responses": { "201": { - "description": "blog created" + "description": "blog created", + "headers": { + } }, "422": { "description": "invalid request", "schema": { "$ref": "#/definitions/errors_object" + }, + "headers": { } } } @@ -136,7 +152,9 @@ ], "responses": { "406": { - "description": "unsupported accept header" + "description": "unsupported accept header", + "headers": { + } } } } @@ -189,7 +207,9 @@ } }, "404": { - "description": "blog not found" + "description": "blog not found", + "headers": { + } } } } @@ -227,7 +247,9 @@ ], "responses": { "200": { - "description": "blog updated" + "description": "blog updated", + "headers": { + } } } }