mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
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:
commit
d32f098e0b
15
README.md
15
README.md
@ -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.
|
||||
|
||||
### 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 ###
|
||||
|
||||
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:
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
module Rswag
|
||||
module Api
|
||||
class Configuration
|
||||
attr_accessor :swagger_root, :swagger_filter
|
||||
attr_accessor :swagger_root, :swagger_filter, :swagger_headers
|
||||
|
||||
def resolve_swagger_root(env)
|
||||
path_params = env['action_dispatch.request.path_parameters'] || {}
|
||||
|
||||
@ -19,11 +19,12 @@ module Rswag
|
||||
swagger = parse_file(filename)
|
||||
@config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
|
||||
mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
|
||||
headers = { 'Content-Type' => mime }.merge(@config.swagger_headers || {})
|
||||
body = unload_swagger(filename, swagger)
|
||||
|
||||
return [
|
||||
'200',
|
||||
{ 'Content-Type' => mime },
|
||||
headers,
|
||||
[ body ]
|
||||
]
|
||||
end
|
||||
|
||||
@ -37,6 +37,42 @@ module Rswag
|
||||
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
|
||||
let(:env) { env_defaults.merge('PATH_INFO' => 'foobar.json') }
|
||||
before do
|
||||
|
||||
Loading…
Reference in New Issue
Block a user