diff --git a/rswag-specs/lib/rswag/specs/example_group_helpers.rb b/rswag-specs/lib/rswag/specs/example_group_helpers.rb index d66a122..812e283 100644 --- a/rswag-specs/lib/rswag/specs/example_group_helpers.rb +++ b/rswag-specs/lib/rswag/specs/example_group_helpers.rb @@ -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 diff --git a/rswag-specs/lib/rswag/specs/swagger_formatter.rb b/rswag-specs/lib/rswag/specs/swagger_formatter.rb index 0faf9cf..a11a69c 100644 --- a/rswag-specs/lib/rswag/specs/swagger_formatter.rb +++ b/rswag-specs/lib/rswag/specs/swagger_formatter.rb @@ -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 diff --git a/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb b/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb index d7e05b1..4ab7777 100644 --- a/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb +++ b/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb @@ -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' } diff --git a/test-app/swagger/v1/swagger.json b/test-app/swagger/v1/swagger.json index 5deaf19..fd380a8 100644 --- a/test-app/swagger/v1/swagger.json +++ b/test-app/swagger/v1/swagger.json @@ -129,7 +129,9 @@ { "name": "keywords", "in": "query", - "type": "string" + "schema": { + "type": "string" + } } ], "responses": { @@ -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": {