Write the files using specified format

This commit is contained in:
Greg Myers 2019-11-02 11:19:01 +00:00
parent 73b84101cc
commit 0e04635b15
2 changed files with 26 additions and 4 deletions

View File

@ -37,7 +37,7 @@ module Rswag
FileUtils.mkdir_p dirname unless File.exists?(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}"
@ -46,6 +46,14 @@ module Rswag
private
def pretty_generate(doc)
if @config.swagger_format == :yaml
YAML.dump(doc)
else # config errors are thrown in 'def swagger_format', no throw needed here
JSON.pretty_generate(doc)
end
end
def metadata_to_swagger(metadata)
response_code = metadata[:response][:code]
response = metadata[:response].reject { |k,v| k == :code }

View File

@ -53,14 +53,28 @@ module Rswag
'v1/swagger.json' => { info: { version: 'v1' } },
'v2/swagger.json' => { info: { version: 'v2' } }
)
allow(config).to receive(:swagger_format).and_return(swagger_format)
subject.stop(notification)
end
let(:notification) { double('notification') }
context 'with default format' do
let(:swagger_format) { :json }
it 'writes the swagger_doc(s) to file' do
expect(File).to exist("#{swagger_root}/v1/swagger.json")
expect(File).to exist("#{swagger_root}/v2/swagger.json")
it 'writes the swagger_doc(s) to file' do
expect(File).to exist("#{swagger_root}/v1/swagger.json")
expect(File).to exist("#{swagger_root}/v2/swagger.json")
expect { JSON.parse(File.read("#{swagger_root}/v2/swagger.json")) }.not_to raise_error
end
end
context 'with yaml format' do
let(:swagger_format) { :yaml }
it 'writes the swagger_doc(s) as yaml' do
expect(File).to exist("#{swagger_root}/v1/swagger.json")
expect { JSON.parse(File.read("#{swagger_root}/v1/swagger.json")) }.to raise_error(JSON::ParserError)
end
end
after do