Adds rswag to test and development so rake tasks work

Adds to swagger_Formatter to remove injected body parameters since those are 2.0 and ont 3.0 compliant

Adds to example_group_helpers to only automatically save request examples in the swagger output on 2xx response, since otherwise it was getting clobbered
This commit is contained in:
Jay Danielian
2019-07-07 22:57:55 -04:00
parent 297cc447c8
commit 0093efd4bf
5 changed files with 66 additions and 186 deletions

View File

@@ -42,6 +42,8 @@ module Rswag
# TODO: setup travis CI?
# MUST HAVES
# TODO: look at integrating and documenting the rest of the responses in the blog_spec and get a clean 3.0 output
# Then can look at handling different request_body things like $ref, etc
# TODO: look at adding request_body method to handle diffs in Open API 2.0 to 3.0
# TODO: look at adding examples in content request_body
# https://swagger.io/docs/specification/describing-request-body/
@@ -123,7 +125,7 @@ module Rswag
end
else
before do |example|
submit_request(example.metadata)
submit_request(example.metadata) #
end
it "returns a #{metadata[:response][:code]} response" do |example|
@@ -137,12 +139,14 @@ module Rswag
if body_parameter && respond_to?(body_parameter[:name]) && example.metadata[:operation][:requestBody][:content]['application/json']
# save response examples by default
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) } unless response.body.to_s.empty?
# save request examples using the let(:param_name) { REQUEST_BODY_HASH } syntax in the test
example.metadata[:operation][:requestBody][:content]['application/json'] = { examples: {} } unless example.metadata[:operation][:requestBody][:content]['application/json'][:examples]
json_request_examples = example.metadata[:operation][:requestBody][:content]['application/json'][:examples]
json_request_examples[body_parameter[:name]] = { value: send(body_parameter[:name]) }
example.metadata[:operation][:requestBody][:content]['application/json'][:examples] = json_request_examples
if response.code.to_s =~ /^2\d{2}$/
example.metadata[:operation][:requestBody][:content]['application/json'] = { examples: {} } unless example.metadata[:operation][:requestBody][:content]['application/json'][:examples]
json_request_examples = example.metadata[:operation][:requestBody][:content]['application/json'][:examples]
json_request_examples[body_parameter[:name]] = { value: send(body_parameter[:name]) }
example.metadata[:operation][:requestBody][:content]['application/json'][:examples] = json_request_examples
end
end
end
end

View File

@@ -34,6 +34,15 @@ module Rswag
def stop(_notification = nil)
@config.swagger_docs.each do |url_path, doc|
# remove 2.0 parameters
doc[:paths].each_pair do |_k, v|
v.each_pair do |_verb, value|
if value&.dig(:parameters)
value[:parameters].reject! { |p| p[:in] == :body }
end
end
end
file_path = File.join(@config.swagger_root, url_path)
dirname = File.dirname(file_path)
FileUtils.mkdir_p dirname unless File.exist?(dirname)
@@ -52,25 +61,19 @@ module Rswag
response_code = metadata[:response][:code]
response = metadata[:response].reject { |k, _v| k == :code }
if response_code.to_s == '201'
# need to merge in to resppnse
if response[:examples]&.dig('application/json')
example = response[:examples].dig('application/json').dup
response.merge!(content: { 'application/json' => { example: example } })
response.delete(:examples)
end
# 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 } })
response.delete(:examples)
end
verb = metadata[:operation][:verb]
operation = metadata[:operation]
.reject { |k, _v| k == :verb }
.merge(responses: { response_code => response })
# can remove the 2.0 compliant body incoming parameters
if operation&.dig(:parameters)
operation[:parameters].reject! { |p| p[:in] == :body }
end
path_template = metadata[:path_item][:template]
path_item = metadata[:path_item]
.reject { |k, _v| k == :template }

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' ]