mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 15:22:56 +00:00
add upgrade path and query param type output to openapi3 if selected
This commit is contained in:
@@ -131,11 +131,6 @@ module Rswag
|
|||||||
attributes[:required] = true
|
attributes[:required] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
## IF OA3
|
|
||||||
# if attributes[:type] && attributes[:schema].nil?
|
|
||||||
# attributes[:schema] = {type: attributes[:type]}
|
|
||||||
# end
|
|
||||||
|
|
||||||
if metadata.has_key?(:operation)
|
if metadata.has_key?(:operation)
|
||||||
metadata[:operation][:parameters] ||= []
|
metadata[:operation][:parameters] ||= []
|
||||||
metadata[:operation][:parameters] << attributes
|
metadata[:operation][:parameters] << attributes
|
||||||
@@ -164,7 +159,7 @@ module Rswag
|
|||||||
|
|
||||||
## OA3
|
## OA3
|
||||||
# if attributes[:type] && attributes[:schema].nil?
|
# if attributes[:type] && attributes[:schema].nil?
|
||||||
# attributes[:schema] = {type: attributes[:type]}
|
# attributes[:schema] = { type: attributes[:type] }
|
||||||
# attributes.delete(:type)
|
# attributes.delete(:type)
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
|||||||
@@ -33,12 +33,16 @@ module Rswag
|
|||||||
return unless metadata.has_key?(:response)
|
return unless metadata.has_key?(:response)
|
||||||
|
|
||||||
swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
|
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))
|
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop(_notification=nil)
|
def stop(_notification=nil)
|
||||||
@config.swagger_docs.each do |url_path, doc|
|
@config.swagger_docs.each do |url_path, doc|
|
||||||
|
|
||||||
## OA3
|
## OA3
|
||||||
# # remove 2.0 parameters
|
# # remove 2.0 parameters
|
||||||
# doc[:paths]&.each_pair do |_k, v|
|
# doc[:paths]&.each_pair do |_k, v|
|
||||||
@@ -115,6 +119,22 @@ module Rswag
|
|||||||
|
|
||||||
{ paths: { path_template => path_item } }
|
{ paths: { path_template => path_item } }
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,12 +22,12 @@ 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) { {} }
|
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
|
||||||
{
|
{
|
||||||
path_item: { template: '/blogs' },
|
path_item: { template: '/blogs', parameters: [{ type: :string }] },
|
||||||
operation: { verb: :post, summary: 'Creates a blog' },
|
operation: { verb: :post, summary: 'Creates a blog', parameters: [{ type: :string }] },
|
||||||
response: { code: '201', description: 'blog created' },
|
response: { code: '201', description: 'blog created' },
|
||||||
document: document
|
document: document
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ module Rswag
|
|||||||
let(:document) { false }
|
let(:document) { false }
|
||||||
|
|
||||||
it 'does not update the swagger doc' do
|
it 'does not update the swagger doc' do
|
||||||
expect(swagger_doc).to be_empty
|
expect(swagger_doc).to match({ swagger: '2.0' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -47,9 +47,35 @@ module Rswag
|
|||||||
|
|
||||||
it 'converts to swagger and merges into the corresponding swagger doc' do
|
it 'converts to swagger and merges into the corresponding swagger doc' do
|
||||||
expect(swagger_doc).to match(
|
expect(swagger_doc).to match(
|
||||||
|
swagger: '2.0',
|
||||||
paths: {
|
paths: {
|
||||||
'/blogs' => {
|
'/blogs' => {
|
||||||
|
parameters: [{ type: :string }],
|
||||||
post: {
|
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',
|
summary: 'Creates a blog',
|
||||||
responses: {
|
responses: {
|
||||||
'201' => { description: 'blog created' }
|
'201' => { description: 'blog created' }
|
||||||
|
|||||||
@@ -129,8 +129,10 @@
|
|||||||
{
|
{
|
||||||
"name": "keywords",
|
"name": "keywords",
|
||||||
"in": "query",
|
"in": "query",
|
||||||
|
"schema": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"406": {
|
"406": {
|
||||||
@@ -144,8 +146,10 @@
|
|||||||
{
|
{
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"type": "string",
|
"required": true,
|
||||||
"required": true
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"get": {
|
"get": {
|
||||||
@@ -195,8 +199,10 @@
|
|||||||
{
|
{
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"in": "path",
|
"in": "path",
|
||||||
"type": "string",
|
"required": true,
|
||||||
"required": true
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"put": {
|
"put": {
|
||||||
@@ -213,8 +219,10 @@
|
|||||||
{
|
{
|
||||||
"name": "file",
|
"name": "file",
|
||||||
"in": "formData",
|
"in": "formData",
|
||||||
"type": "file",
|
"required": true,
|
||||||
"required": true
|
"schema": {
|
||||||
|
"type": "file"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
|
|||||||
Reference in New Issue
Block a user