Allow parsing of yml swagger files in rswag-api

This commit is contained in:
Ben Lewis 2019-02-06 11:43:02 +00:00
parent a50bf616b9
commit 29c9f7cae2
3 changed files with 34 additions and 1 deletions

View File

@ -1,4 +1,5 @@
require 'json'
require 'yaml'
module Rswag
module Api
@ -14,7 +15,7 @@ module Rswag
filename = "#{@config.resolve_swagger_root(env)}/#{path}"
if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
swagger = load_json(filename)
swagger = parse_file(filename)
@config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
return [
@ -29,6 +30,18 @@ module Rswag
private
def parse_file(filename)
if /\.yml$/ === filename
load_yaml(filename)
else
load_json(filename)
end
end
def load_yaml(filename)
YAML.safe_load(File.read(filename))
end
def load_json(filename)
JSON.parse(File.read(filename))
end

View File

@ -0,0 +1,5 @@
swagger: '2.0'
info:
title: API V1
version: v1
paths: {}

View File

@ -76,6 +76,21 @@ module Rswag
expect(response[2].join).to include('"host":"tempuri.org"')
end
end
context 'when a path maps to a yaml swagger file' do
let(:env) { env_defaults.merge('PATH_INFO' => 'v1/swagger.yml') }
it 'returns a 200 status' do
expect(response.length).to eql(3)
expect(response.first).to eql('200')
end
it 'returns contents of the swagger file' do
expect(response.length).to eql(3)
expect(response[1]).to include( 'Content-Type' => 'application/json')
expect(response[2].join).to include('"title":"API V1"')
end
end
end
end
end