add upgrade for basepath and host to server urls

This commit is contained in:
Greg Myers 2020-03-22 00:04:11 +00:00
parent da230a4f3e
commit eb58fe687a
3 changed files with 59 additions and 13 deletions

View File

@ -36,6 +36,7 @@ module Rswag
if !doc_version(swagger_doc).start_with?('2') if !doc_version(swagger_doc).start_with?('2')
upgrade_request_type!(metadata) upgrade_request_type!(metadata)
upgrade_servers!(swagger_doc)
end end
swagger_doc.deep_merge!(metadata_to_swagger(metadata)) swagger_doc.deep_merge!(metadata_to_swagger(metadata))
@ -136,6 +137,20 @@ module Rswag
end end
end 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 end
end end

View File

@ -22,7 +22,6 @@ module Rswag
allow(config).to receive(:get_swagger_doc).and_return(swagger_doc) allow(config).to receive(:get_swagger_doc).and_return(swagger_doc)
subject.example_group_finished(notification) subject.example_group_finished(notification)
end end
let(:swagger_doc) { { swagger: '2.0' } }
let(:notification) { OpenStruct.new(group: OpenStruct.new(metadata: api_metadata)) } let(:notification) { OpenStruct.new(group: OpenStruct.new(metadata: api_metadata)) }
let(:api_metadata) do let(:api_metadata) do
{ {
@ -34,6 +33,7 @@ module Rswag
end end
context 'with the document tag set to false' do context 'with the document tag set to false' do
let(:swagger_doc) { { swagger: '2.0' } }
let(:document) { false } let(:document) { false }
it 'does not update the swagger doc' do it 'does not update the swagger doc' do
@ -42,6 +42,7 @@ module Rswag
end end
context 'with the document tag set to anything but false' do 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. # anything works, including its absence when specifying responses.
let(:document) { nil } let(:document) { nil }
@ -67,13 +68,21 @@ module Rswag
end end
end end
context 'upgrades to 3.0' do context 'with metadata upgrades for 3.0' do
let(:swagger_doc) { { openapi: '3.0.1'} } let(:swagger_doc) { {
openapi: '3.0.1',
basePath: '/foo',
schemes: ['http', 'https'],
host: 'api.example.com'
} }
let(:document) { nil } let(:document) { nil }
it 'converts query and path params, type: to schema: { type: }' do it 'converts query and path params, type: to schema: { type: }' do
expect(swagger_doc).to match( expect(swagger_doc).to match(
openapi: '3.0.1', openapi: '3.0.1',
servers: {
urls: ['http://api.example.com/foo', 'https://api.example.com/foo']
},
paths: { paths: {
'/blogs' => { '/blogs' => {
parameters: [{ schema: { type: :string } }], parameters: [{ schema: { type: :string } }],

View File

@ -21,10 +21,14 @@
], ],
"responses": { "responses": {
"204": { "204": {
"description": "Valid credentials" "description": "Valid credentials",
"headers": {
}
}, },
"401": { "401": {
"description": "Invalid credentials" "description": "Invalid credentials",
"headers": {
}
} }
} }
} }
@ -45,10 +49,14 @@
], ],
"responses": { "responses": {
"204": { "204": {
"description": "Valid credentials" "description": "Valid credentials",
"headers": {
}
}, },
"401": { "401": {
"description": "Invalid credentials" "description": "Invalid credentials",
"headers": {
}
} }
} }
} }
@ -72,10 +80,14 @@
], ],
"responses": { "responses": {
"204": { "204": {
"description": "Valid credentials" "description": "Valid credentials",
"headers": {
}
}, },
"401": { "401": {
"description": "Invalid credentials" "description": "Invalid credentials",
"headers": {
}
} }
} }
} }
@ -105,12 +117,16 @@
], ],
"responses": { "responses": {
"201": { "201": {
"description": "blog created" "description": "blog created",
"headers": {
}
}, },
"422": { "422": {
"description": "invalid request", "description": "invalid request",
"schema": { "schema": {
"$ref": "#/definitions/errors_object" "$ref": "#/definitions/errors_object"
},
"headers": {
} }
} }
} }
@ -136,7 +152,9 @@
], ],
"responses": { "responses": {
"406": { "406": {
"description": "unsupported accept header" "description": "unsupported accept header",
"headers": {
}
} }
} }
} }
@ -189,7 +207,9 @@
} }
}, },
"404": { "404": {
"description": "blog not found" "description": "blog not found",
"headers": {
}
} }
} }
} }
@ -227,7 +247,9 @@
], ],
"responses": { "responses": {
"200": { "200": {
"description": "blog updated" "description": "blog updated",
"headers": {
}
} }
} }
} }