mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
Allow parsing of yml swagger files in rswag-api
This commit is contained in:
parent
a50bf616b9
commit
29c9f7cae2
@ -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
|
||||
|
||||
5
rswag-api/spec/rswag/api/fixtures/swagger/v1/swagger.yml
Normal file
5
rswag-api/spec/rswag/api/fixtures/swagger/v1/swagger.yml
Normal file
@ -0,0 +1,5 @@
|
||||
swagger: '2.0'
|
||||
info:
|
||||
title: API V1
|
||||
version: v1
|
||||
paths: {}
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user