mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Write the files using specified format
This commit is contained in:
parent
73b84101cc
commit
0e04635b15
@ -37,7 +37,7 @@ module Rswag
|
|||||||
FileUtils.mkdir_p dirname unless File.exists?(dirname)
|
FileUtils.mkdir_p dirname unless File.exists?(dirname)
|
||||||
|
|
||||||
File.open(file_path, 'w') do |file|
|
File.open(file_path, 'w') do |file|
|
||||||
file.write(JSON.pretty_generate(doc))
|
file.write(pretty_generate(doc))
|
||||||
end
|
end
|
||||||
|
|
||||||
@output.puts "Swagger doc generated at #{file_path}"
|
@output.puts "Swagger doc generated at #{file_path}"
|
||||||
@ -46,6 +46,14 @@ module Rswag
|
|||||||
|
|
||||||
private
|
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)
|
def metadata_to_swagger(metadata)
|
||||||
response_code = metadata[:response][:code]
|
response_code = metadata[:response][:code]
|
||||||
response = metadata[:response].reject { |k,v| k == :code }
|
response = metadata[:response].reject { |k,v| k == :code }
|
||||||
|
|||||||
@ -53,14 +53,28 @@ module Rswag
|
|||||||
'v1/swagger.json' => { info: { version: 'v1' } },
|
'v1/swagger.json' => { info: { version: 'v1' } },
|
||||||
'v2/swagger.json' => { info: { version: 'v2' } }
|
'v2/swagger.json' => { info: { version: 'v2' } }
|
||||||
)
|
)
|
||||||
|
allow(config).to receive(:swagger_format).and_return(swagger_format)
|
||||||
subject.stop(notification)
|
subject.stop(notification)
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:notification) { double('notification') }
|
let(:notification) { double('notification') }
|
||||||
|
context 'with default format' do
|
||||||
|
let(:swagger_format) { :json }
|
||||||
|
|
||||||
it 'writes the swagger_doc(s) to file' do
|
it 'writes the swagger_doc(s) to file' do
|
||||||
expect(File).to exist("#{swagger_root}/v1/swagger.json")
|
expect(File).to exist("#{swagger_root}/v1/swagger.json")
|
||||||
expect(File).to exist("#{swagger_root}/v2/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
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user