For my Rails project I am using RSwag to generate documentation using OpenAPI 3.0.3 specifications.
I know with Open API 3.0.3 'If in is "header" and the name field is "Accept", "Content-Type" or "Authorization", the parameter definition SHALL be ignored.'
However I know they can be generated by configuring the rspec
and swagger_helper.rb
files properly. For example, my swagger_helper.rb
has
config.swagger_docs = {
'v1/swagger.json' => {
openapi: '3.0.3',
info: {
title: 'My company API',
version: 'v1'
},
servers: [
{
url: "#{ENV['PROTOCOL']}://#{ENV['BINDING']}:#{ENV['PORT']}"
}
],
components: {
contentType: 'application/vnd.api+json',
headers: {
contentType: {
description: "content type",
schema: {
type: :string
}
}
},
securitySchemes: {
authorization_header: {
type: :apiKey,
name: 'Authorization',
in: :header,
description: "Expected format: app_name:api_key"
}
},
schemas: {
errors_list: {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"errors": {
"type": "array",
"items": [
{}
]
}
},
"required": [
"errors"
]
}
}
},
encoding: {
contentType: 'application/vnd.v1+json'
},
mediaType: {
schema: {
type: :string
},
example: 'application/vnd.v1+json',
encoding: {
contentType: 'application/vnd.v1+json'
}
}
}
}
and one of my rspec file has
path '/api/segments/{id}', swagger_doc: 'v1/swagger.json' do
get 'Show segment by id' do
after do |example|
example.metadata[:response][:content] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
end
let(:segment) { create(:segment, user: user) }
let(:id) { segment.id }
let(:Authorization) { auth_header }
produces 'application/vnd.my_company.v1+json'
consumes 'application/vnd.api+json'
security [ authorization_header: [] ]
tags :segments
parameter name: :Accept, in: :header, type: :string, required: true, example: 'application/vnd.my_company.v1+json'
parameter name: 'Content-Type', in: :header, type: :string, required: true, example: 'application/vnd.api+json'
parameter name: :Authorization, in: :header, type: :string, required: true
parameter name: :id, in: :path, type: :integer , required: true, description: 'id of segment'
parameter name: :marketplace, in: :query, type: :string, schema: { type: :string }, required: true, description: 'marketplace of segment'
context 'abc' do
response '200', :success do
run_test! do |response|
xxxx
end
end
end
end
With the definitions for securitySchemes
and using it with security [ authorization_header: [] ]
, as well as using produces 'application/vnd.my_company.v1+json'
, I am able to send Authorization
and Accept
headers on my swagger UI page using the "Try it out" functionality.
However, I am not able to send the Content-Type
header. Where did I do wrong?
I know if I use Swagger 2.0 instead of OpenAPI 3.0.3 this problem will not occur, but I don't want to switch.
Update:
I manually added:
"requestBody": {
"content": {
"application/vnd.api+json": {}
}
},
underneath
"paths": {
"/api/segments/{id}": {
"get": {
"summary": "Show segment by id",
"security": [
{
"authorization_header": [
]
}
],
"tags": [
"segments"
],
"requestBody": {
"content": {
"application/vnd.api+json": {}
}
},
in my swagger.json
file and now it lets me send Content-Type
header!
But how do I do it in my swagger_helper.rb
or the rspec
file?
Thank you!