我正在使用 gem rswag 作为 Rails API 的文档。我的 rails API 要求所有请求都包括headers['Content-Type']='my-fancy-content-type'
和headers['Accept']='my-fancy-accept
. 到目前为止,我已经使用 POSTMAN 进行了测试,一切正常。我的 Rspec 测试也都通过了。
在http://localhost:3002/api-docs/index.html
. 我输入了正确的参数(在这种情况下只是 id)和正确的标题,如屏幕截图所示:
似乎没有发送任何标题。控制台显示它curl -X GET "http://localhost:3002/segments/122" -H "accept: */*"
没有任何标题。
我设置了我的spec/swagger_helper.rb
喜欢:
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
我的 swagger.yaml 看起来像:
---
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
用于生成 swagger 文档的测试片段是(它正常通过):
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
我尝试c.swagger_headers = { 'Content-Type' => 'my-fancy-content-type', 'Accept' => 'my-fancy-accept' }
在里面添加 Rswag::Api.configconfig/initializers/rswag_api.rb
但它不起作用(即不发送任何标题)。
在此先感谢您的帮助!