add upgrade path and query param type output to openapi3 if selected

This commit is contained in:
Greg Myers 2020-03-21 21:27:22 +00:00
parent 23a1074d07
commit 70eb277e04
4 changed files with 67 additions and 18 deletions

View File

@ -131,11 +131,6 @@ module Rswag
attributes[:required] = true
end
## IF OA3
# if attributes[:type] && attributes[:schema].nil?
# attributes[:schema] = {type: attributes[:type]}
# end
if metadata.has_key?(:operation)
metadata[:operation][:parameters] ||= []
metadata[:operation][:parameters] << attributes
@ -164,7 +159,7 @@ module Rswag
## OA3
# if attributes[:type] && attributes[:schema].nil?
# attributes[:schema] = {type: attributes[:type]}
# attributes[:schema] = { type: attributes[:type] }
# attributes.delete(:type)
# end

View File

@ -33,12 +33,16 @@ module Rswag
return unless metadata.has_key?(:response)
swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
if doc_version(swagger_doc).starts_with?('3')
upgrade_request_type!(metadata)
end
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
end
def stop(_notification=nil)
@config.swagger_docs.each do |url_path, doc|
## OA3
# # remove 2.0 parameters
# doc[:paths]&.each_pair do |_k, v|
@ -115,6 +119,22 @@ module Rswag
{ paths: { path_template => path_item } }
end
def doc_version(doc)
doc[:openapi] || doc[:swagger] || '3'
end
def upgrade_request_type!(metadata)
operation_nodes = metadata[:operation][:parameters] || []
path_nodes = metadata[:path_item][:parameters] || []
(operation_nodes + path_nodes).each do |node|
if node && node[:type] && node[:schema].nil?
node[:schema] = { type: node[:type] }
node.delete(:type)
end
end
end
end
end
end

View File

@ -22,12 +22,12 @@ module Rswag
allow(config).to receive(:get_swagger_doc).and_return(swagger_doc)
subject.example_group_finished(notification)
end
let(:swagger_doc) { {} }
let(:swagger_doc) { { swagger: '2.0' } }
let(:notification) { OpenStruct.new(group: OpenStruct.new(metadata: api_metadata)) }
let(:api_metadata) do
{
path_item: { template: '/blogs' },
operation: { verb: :post, summary: 'Creates a blog' },
path_item: { template: '/blogs', parameters: [{ type: :string }] },
operation: { verb: :post, summary: 'Creates a blog', parameters: [{ type: :string }] },
response: { code: '201', description: 'blog created' },
document: document
}
@ -37,7 +37,7 @@ module Rswag
let(:document) { false }
it 'does not update the swagger doc' do
expect(swagger_doc).to be_empty
expect(swagger_doc).to match({ swagger: '2.0' })
end
end
@ -47,9 +47,35 @@ module Rswag
it 'converts to swagger and merges into the corresponding swagger doc' do
expect(swagger_doc).to match(
swagger: '2.0',
paths: {
'/blogs' => {
parameters: [{ type: :string }],
post: {
parameters: [{ type: :string }],
summary: 'Creates a blog',
responses: {
'201' => { description: 'blog created' }
}
}
}
}
)
end
end
context 'upgrades to 3.0' do
let(:swagger_doc) { { openapi: '3.0.1'} }
let(:document) { nil }
it 'converts query and path params, type: to schema: { type: }' do
expect(swagger_doc).to match(
openapi: '3.0.1',
paths: {
'/blogs' => {
parameters: [{ schema: { type: :string } }],
post: {
parameters: [{ schema: { type: :string } }],
summary: 'Creates a blog',
responses: {
'201' => { description: 'blog created' }

View File

@ -129,8 +129,10 @@
{
"name": "keywords",
"in": "query",
"schema": {
"type": "string"
}
}
],
"responses": {
"406": {
@ -144,8 +146,10 @@
{
"name": "id",
"in": "path",
"type": "string",
"required": true
"required": true,
"schema": {
"type": "string"
}
}
],
"get": {
@ -195,8 +199,10 @@
{
"name": "id",
"in": "path",
"type": "string",
"required": true
"required": true,
"schema": {
"type": "string"
}
}
],
"put": {
@ -213,8 +219,10 @@
{
"name": "file",
"in": "formData",
"type": "file",
"required": true
"required": true,
"schema": {
"type": "file"
}
}
],
"responses": {