mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
Serve yaml files as yaml instead of converting them to json
This commit is contained in:
parent
eeb1026691
commit
dc161fe275
@ -1,5 +1,6 @@
|
|||||||
require 'json'
|
require 'json'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
require 'rack/mime'
|
||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Api
|
module Api
|
||||||
@ -17,11 +18,13 @@ module Rswag
|
|||||||
if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
|
if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
|
||||||
swagger = parse_file(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?
|
||||||
|
mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
|
||||||
|
body = unload_swagger(filename, swagger)
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'200',
|
'200',
|
||||||
{ 'Content-Type' => 'application/json' },
|
{ 'Content-Type' => mime },
|
||||||
[ JSON.dump(swagger) ]
|
[ body ]
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -45,6 +48,14 @@ module Rswag
|
|||||||
def load_json(filename)
|
def load_json(filename)
|
||||||
JSON.parse(File.read(filename))
|
JSON.parse(File.read(filename))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unload_swagger(filename, swagger)
|
||||||
|
if /\.ya?ml$/ === filename
|
||||||
|
YAML.dump(swagger)
|
||||||
|
else
|
||||||
|
JSON.dump(swagger)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -87,8 +87,8 @@ module Rswag
|
|||||||
|
|
||||||
it 'returns contents of the swagger file' do
|
it 'returns contents of the swagger file' do
|
||||||
expect(response.length).to eql(3)
|
expect(response.length).to eql(3)
|
||||||
expect(response[1]).to include( 'Content-Type' => 'application/json')
|
expect(response[1]).to include( 'Content-Type' => 'text/yaml')
|
||||||
expect(response[2].join).to include('"title":"API V1"')
|
expect(response[2].join).to include('title: API V1')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,9 +2,9 @@ Rswag::Ui.configure do |c|
|
|||||||
|
|
||||||
# List the Swagger endpoints that you want to be documented through the swagger-ui
|
# List the Swagger endpoints that you want to be documented through the swagger-ui
|
||||||
# The first parameter is the path (absolute or relative to the UI host) to the corresponding
|
# The first parameter is the path (absolute or relative to the UI host) to the corresponding
|
||||||
# JSON endpoint and the second is a title that will be displayed in the document selector
|
# endpoint and the second is a title that will be displayed in the document selector
|
||||||
# NOTE: If you're using rspec-api to expose Swagger files (under swagger_root) as JSON endpoints,
|
# NOTE: If you're using rspec-api to expose Swagger files (under swagger_root) as JSON or YAML endpoints,
|
||||||
# then the list below should correspond to the relative paths for those endpoints
|
# then the list below should correspond to the relative paths for those endpoints
|
||||||
|
|
||||||
c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'
|
c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs'
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user