mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 15:22:56 +00:00
Add yaml support (#7)
This commit is contained in:
committed by
jdanielian
parent
79304a1bc1
commit
f331e064fd
@@ -32,4 +32,10 @@ RSpec.configure do |config|
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
# Specify the format of the output Swagger file when running 'rswag:specs:swaggerize'.
|
||||
# The swagger_docs configuration option has the filename including format in
|
||||
# the key, this may want to be changed to avoid putting yaml in json files.
|
||||
# Defaults to json. Accepts ':json' and ':yaml'.
|
||||
config.swagger_format = :yaml
|
||||
end
|
||||
|
||||
@@ -13,6 +13,7 @@ module OpenApi
|
||||
c.add_setting :swagger_root
|
||||
c.add_setting :swagger_docs
|
||||
c.add_setting :swagger_dry_run
|
||||
c.add_setting :swagger_format
|
||||
c.extend ExampleGroupHelpers, type: :request
|
||||
c.include ExampleHelpers, type: :request
|
||||
end
|
||||
|
||||
@@ -34,6 +34,14 @@ module OpenApi
|
||||
end
|
||||
end
|
||||
|
||||
def swagger_format
|
||||
@swagger_format ||= begin
|
||||
@rspec_config.swagger_format = :json if @rspec_config.swagger_format.nil? || @rspec_config.swagger_format.empty?
|
||||
raise ConfigurationError, "Unknown swagger_format '#{@rspec_config.swagger_format}'" unless [:json, :yaml].include?(@rspec_config.swagger_format)
|
||||
@rspec_config.swagger_format
|
||||
end
|
||||
end
|
||||
|
||||
def get_swagger_doc(name)
|
||||
return swagger_docs.values.first if name.nil?
|
||||
raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name]
|
||||
|
||||
@@ -60,7 +60,7 @@ module OpenApi
|
||||
FileUtils.mkdir_p dirname unless File.exist?(dirname)
|
||||
|
||||
File.open(file_path, 'w') do |file|
|
||||
file.write(JSON.pretty_generate(doc))
|
||||
file.write(pretty_generate(doc))
|
||||
end
|
||||
|
||||
@output.puts "Swagger doc generated at #{file_path}"
|
||||
@@ -69,6 +69,20 @@ module OpenApi
|
||||
|
||||
private
|
||||
|
||||
def pretty_generate(doc)
|
||||
if @config.swagger_format == :yaml
|
||||
clean_doc = yaml_prepare(doc)
|
||||
YAML.dump(clean_doc)
|
||||
else # config errors are thrown in 'def swagger_format', no throw needed here
|
||||
JSON.pretty_generate(doc)
|
||||
end
|
||||
end
|
||||
|
||||
def yaml_prepare(doc)
|
||||
json_doc = JSON.pretty_generate(doc)
|
||||
JSON.parse(json_doc)
|
||||
end
|
||||
|
||||
def metadata_to_swagger(metadata)
|
||||
response_code = metadata[:response][:code]
|
||||
response = metadata[:response].reject { |k, _v| k == :code }
|
||||
|
||||
Reference in New Issue
Block a user