微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rails Rswag Swagger UI无法正确发送标题

如何解决Rails Rswag Swagger UI无法正确发送标题

我将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使用大头UI时。我输入了正确的参数(在本例中为id)和正确的标题,如屏幕截图所示:

swagger UI

似乎没有头发送。控制台显示它正在做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

用于生成摇摇欲坠的文档的测试片段是(正常通过):

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',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.config里面添加config/initializers/rswag_api.rb,但是没有用(即不发送任何标题)。

在此先感谢您的帮助!

解决方法

在我的spec/swagger_helper.rb中,我不再使用Open API,而是使用Swagger 2.0。所以改变

config.swagger_docs = {
    'v1/swagger.yml' => {
      openapi: '3.0.1',

  config.swagger_docs = {
    'v1/swagger.json' => {
      swagger: '2.0',

现在它可以工作了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?