mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
Merge pull request #190 from bclewis1/allow-yaml-parsing-in-rswag-api
Allow parsing of yml swagger files in rswag-api
This commit is contained in:
commit
d84a89510d
@ -1,4 +1,5 @@
|
|||||||
require 'json'
|
require 'json'
|
||||||
|
require 'yaml'
|
||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Api
|
module Api
|
||||||
@ -14,7 +15,7 @@ module Rswag
|
|||||||
filename = "#{@config.resolve_swagger_root(env)}/#{path}"
|
filename = "#{@config.resolve_swagger_root(env)}/#{path}"
|
||||||
|
|
||||||
if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
|
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?
|
@config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@ -29,6 +30,18 @@ module Rswag
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def parse_file(filename)
|
||||||
|
if /\.ya?ml$/ === 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)
|
def load_json(filename)
|
||||||
JSON.parse(File.read(filename))
|
JSON.parse(File.read(filename))
|
||||||
end
|
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"')
|
expect(response[2].join).to include('"host":"tempuri.org"')
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user