mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
allow headers to be set in the configuration of rswag-api
This commit is contained in:
parent
a50bf616b9
commit
d00bb06e19
17
README.md
17
README.md
@ -44,9 +44,9 @@ Once you have an API that can describe itself in Swagger, you've opened the trea
|
|||||||
```ruby
|
```ruby
|
||||||
rails g rswag:install
|
rails g rswag:install
|
||||||
```
|
```
|
||||||
|
|
||||||
Or run the install generators for each package separately if you installed Rswag as separate gems, as indicated above:
|
Or run the install generators for each package separately if you installed Rswag as separate gems, as indicated above:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
rails g rswag:api:install rswag:ui:install
|
rails g rswag:api:install rswag:ui:install
|
||||||
RAILS_ENV=test rails g rswag:specs:install
|
RAILS_ENV=test rails g rswag:specs:install
|
||||||
@ -466,6 +466,19 @@ 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 Authoriation 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 Authoriation 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
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### 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:
|
||||||
|
|||||||
@ -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'] || {}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ require 'json'
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Api
|
module Api
|
||||||
class Middleware
|
class Middleware
|
||||||
|
|
||||||
def initialize(app, config)
|
def initialize(app, config)
|
||||||
@app = app
|
@app = app
|
||||||
@ -16,14 +16,15 @@ module Rswag
|
|||||||
if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
|
if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
|
||||||
swagger = load_json(filename)
|
swagger = load_json(filename)
|
||||||
@config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
|
@config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
|
||||||
|
headers = { 'Content-Type' => 'application/json' }.merge(@config.swagger_headers || {})
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'200',
|
'200',
|
||||||
{ 'Content-Type' => 'application/json' },
|
headers,
|
||||||
[ JSON.dump(swagger) ]
|
[ JSON.dump(swagger) ]
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
return @app.call(env)
|
return @app.call(env)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user