diff --git a/README.md b/README.md index 8a01132..317eaeb 100644 --- a/README.md +++ b/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: diff --git a/rswag-api/lib/rswag/api/configuration.rb b/rswag-api/lib/rswag/api/configuration.rb index ff56180..bf64229 100644 --- a/rswag-api/lib/rswag/api/configuration.rb +++ b/rswag-api/lib/rswag/api/configuration.rb @@ -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'] || {} diff --git a/rswag-api/lib/rswag/api/middleware.rb b/rswag-api/lib/rswag/api/middleware.rb index 637d42d..77a3b01 100644 --- a/rswag-api/lib/rswag/api/middleware.rb +++ b/rswag-api/lib/rswag/api/middleware.rb @@ -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 diff --git a/rswag-api/spec/rswag/api/middleware_spec.rb b/rswag-api/spec/rswag/api/middleware_spec.rb index 3ff0594..049b3fd 100644 --- a/rswag-api/spec/rswag/api/middleware_spec.rb +++ b/rswag-api/spec/rswag/api/middleware_spec.rb @@ -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