Merge pull request #187 from pjdavis/configure-response-header-in-rswag-api

allow headers to be set in the configuration of rswag-api
This commit is contained in:
Greg Myers 2020-04-04 20:01:04 +01:00 committed by GitHub
commit d32f098e0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 2 deletions

View File

@ -534,6 +534,21 @@ end
Note how the filter is passed the rack env for the current request. This provides a lot of flexibilty. For example, you can assign the "host" property (as shown) or you could inspect session information or an Authorization header and remove operations based on user permissions. Note how the filter is passed the rack env for the current request. This provides a lot of flexibilty. For example, you can assign the "host" property (as shown) or you could inspect session information or an Authorization header and remove operations based on user permissions.
### Custom Headers for Swagger Files ###
You can specify custom headers for serving your generated Swagger JSON. For example you may want to force a specific charset for the 'Content-Type' header. You can configure a hash of headers to be sent with the request:
```ruby
Rswag::Api.configure do |c|
...
c.swagger_headers = { 'Content-Type' => 'application/json; charset=UTF-8' }
end
```
Take care when overriding Content-Type if you serve both YAML and JSON files as it will no longer switch the Content-Type header correctly.
### Enable Swagger Endpoints for swagger-ui ### ### Enable Swagger Endpoints for swagger-ui ###
You can update the _rswag-ui.rb_ initializer, installed with rswag-ui, to specify which Swagger endpoints should be available to power the documentation UI. If you're using rswag-api, these should correspond to the Swagger endpoints it exposes. When the UI is rendered, you'll see these listed in a drop-down to the top right of the page: You can update the _rswag-ui.rb_ initializer, installed with rswag-ui, to specify which Swagger endpoints should be available to power the documentation UI. If you're using rswag-api, these should correspond to the Swagger endpoints it exposes. When the UI is rendered, you'll see these listed in a drop-down to the top right of the page:

View File

@ -1,7 +1,7 @@
module Rswag module Rswag
module Api module Api
class Configuration class Configuration
attr_accessor :swagger_root, :swagger_filter attr_accessor :swagger_root, :swagger_filter, :swagger_headers
def resolve_swagger_root(env) def resolve_swagger_root(env)
path_params = env['action_dispatch.request.path_parameters'] || {} path_params = env['action_dispatch.request.path_parameters'] || {}

View File

@ -19,11 +19,12 @@ module Rswag
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') mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
headers = { 'Content-Type' => mime }.merge(@config.swagger_headers || {})
body = unload_swagger(filename, swagger) body = unload_swagger(filename, swagger)
return [ return [
'200', '200',
{ 'Content-Type' => mime }, headers,
[ body ] [ body ]
] ]
end end

View File

@ -37,6 +37,42 @@ module Rswag
end end
end end
context 'when swagger_headers is configured' do
let(:env) { env_defaults.merge('PATH_INFO' => 'v1/swagger.json') }
context 'replacing the default content type header' do
before do
config.swagger_headers = { 'Content-Type' => 'application/json; charset=UTF-8' }
end
it 'returns a 200 status' do
expect(response.length).to eql(3)
expect(response.first).to eql('200')
end
it 'applies the headers to the response' do
expect(response[1]).to include( 'Content-Type' => 'application/json; charset=UTF-8')
end
end
context 'adding an additional header' do
before do
config.swagger_headers = { 'Access-Control-Allow-Origin' => '*' }
end
it 'returns a 200 status' do
expect(response.length).to eql(3)
expect(response.first).to eql('200')
end
it 'applies the headers to the response' do
expect(response[1]).to include( 'Access-Control-Allow-Origin' => '*')
end
it 'keeps the default header' do
expect(response[1]).to include( 'Content-Type' => 'application/json')
end
end
end
context "given a path that doesn't map to any swagger file" do context "given a path that doesn't map to any swagger file" do
let(:env) { env_defaults.merge('PATH_INFO' => 'foobar.json') } let(:env) { env_defaults.merge('PATH_INFO' => 'foobar.json') }
before do before do