Modifies parameters and body request/responses to output 3.0 syntax for basic operations.

SwaggerEditor passes basic output
This commit is contained in:
Jay Danielian
2019-07-14 17:28:11 -04:00
parent 23349b2678
commit c820bb75e0
7 changed files with 226 additions and 85 deletions

View File

@@ -69,7 +69,7 @@ module Rswag
example_key_name = passed_examples.first # can come up with better scheme here
# TODO: write more tests around this adding to the parameter
# if symbol try and use save_request_example
param_attributes = { name: example_key_name, in: :body, required: required, param_value: example_key_name }
param_attributes = { name: example_key_name, in: :body, required: required, param_value: example_key_name, schema: schema }
parameter(param_attributes)
end
end
@@ -80,6 +80,10 @@ module Rswag
attributes[:required] = true
end
if attributes[:type] && attributes[:schema].nil?
attributes[:schema] = {type: attributes[:type]}
end
if metadata.key?(:operation)
metadata[:operation][:parameters] ||= []
metadata[:operation][:parameters] << attributes
@@ -94,12 +98,19 @@ module Rswag
context(description, metadata, &block)
end
def schema(value)
metadata[:response][:schema] = value
def schema(value, content_type: 'application/json')
content_hash = {content_type => {schema: value}}
metadata[:response][:content] = content_hash
end
def header(name, attributes)
metadata[:response][:headers] ||= {}
if attributes[:type] && attributes[:schema].nil?
attributes[:schema] = {type: attributes[:type]}
attributes.delete(:type)
end
metadata[:response][:headers][name] = attributes
end

View File

@@ -37,9 +37,20 @@ module Rswag
# remove 2.0 parameters
doc[:paths].each_pair do |_k, v|
v.each_pair do |_verb, value|
if value&.dig(:parameters)
is_hash = value.is_a?(Hash)
if is_hash && value.dig(:parameters)
schema_param = value&.dig(:parameters)&.find{|p| p[:in] == :body && p[:schema] }
if value && schema_param && value&.dig(:requestBody, :content, 'application/json')
value[:requestBody][:content]['application/json'].merge!(schema: schema_param[:schema])
end
value[:parameters].reject! { |p| p[:in] == :body }
value[:parameters].each { |p| p.delete(:type) }
value[:headers].each { |p| p.delete(:type)} if value[:headers]
end
value.delete(:consumes) if is_hash && value.dig(:consumes)
value.delete(:produces) if is_hash && value.dig(:produces)
end
end
@@ -64,7 +75,10 @@ module Rswag
# need to merge in to response
if response[:examples]&.dig('application/json')
example = response[:examples].dig('application/json').dup
response.merge!(content: { 'application/json' => { example: example } })
schema = response.dig(:content, 'application/json', :schema)
new_hash = {example: example}
new_hash[:schema] = schema if schema
response.merge!(content: { 'application/json' => new_hash })
response.delete(:examples)
end

View File

@@ -6,7 +6,7 @@ namespace :rswag do
desc 'Generate Swagger JSON files from integration specs'
RSpec::Core::RakeTask.new('swaggerize') do |t|
t.pattern = 'spec/requests/**/*_spec.rb, spec/api/**/*_spec.rb, spec/integration/**/*_spec.rb'
# TODO: fix this, as dry-run is always true despite what is in the config
# NOTE: rspec 2.x support
if Rswag::Specs::RSPEC_VERSION > 2 && Rswag::Specs.config.swagger_dry_run
t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--dry-run', '--order defined' ]