I'm using gem rswag for documentation for my Rails API. My rails API requires that all requests include headers['Content-Type']='my-fancy-content-type'
and headers['Accept']='my-fancy-accept
. So far I've done testing using POSTMAN and all are working fine. My Rspec tests also all pass.
When using the swagger UI at http://localhost:3002/api-docs/index.html
. I input the correct param (just id in this case), and the correct headers as shown in the screenshot:
It seems that none of the headers were sent. The console shows it was doing curl -X GET "http://localhost:3002/segments/122" -H "accept: */*"
without any headers.
I set up my spec/swagger_helper.rb
like:
require 'rails_helper'
RSpec.configure do |config|
config.swagger_root = Rails.root.join('swagger').to_s
config.swagger_docs = {
'v1/swagger.yml' => {
openapi: '3.0.1',
info: {
title: 'API V1',
version: 'v1'
},
paths: {},
servers: [
{
url: 'http://{defaultHost}',
variables: {
defaultHost: {
default: "localhost:#{ENV['PORT']}"
}
}
}
]
}
}
config.swagger_format = :yaml
end
My swagger.yaml looks like:
---
openapi: 3.0.1
info:
title: API V1
version: v1
paths:
"/segments/{id}":
get:
summary: Find segment by id
tags:
- Segments
parameters:
- name: Accept
in: header
required: true
schema:
type: string
- name: Content-Type
in: header
required: true
schema:
type: string
- name: id
in: path
required: true
schema:
type: integer
responses:
'200':
description: success
content: {}
'404':
description: not_found
content: {}
servers:
- url: http://{defaultHost}
variables:
defaultHost:
default: localhost:3002
securityDefinitions:
Content-Type:
type: string
description: Content-Type
name: Content-Type
in: header
A snippet of a test used to generate the swagger documentation is(it passes normally):
path '/segments/{id}' do
get 'Find segment by id' do
tags 'Segments'
parameter name: :Accept, in: :header, type: :string, required: true
parameter name: 'Content-Type', in: :header, type: :string, required: true
parameter name: :id, in: :path, type: :integer
context 'when record exists' do
response '200', :success do
schema type: :object,
properties: {
content: {
id: {type: :string},
type: {type: :string},
attributes: {
id: :integer,
...
}
}
}
run_test! do |response|
expect(response.status).to eq(200)
end
end
I tried adding c.swagger_headers = { 'Content-Type' => 'my-fancy-content-type', 'Accept' => 'my-fancy-accept' }
inside Rswag::Api.config inside config/initializers/rswag_api.rb
but it did not work (i.e. not sending any of the headers).
Thanks in advance for your help!