带有 RSwag 无法发送“Content-Type”标头的 OpenAPI 3.0.3 文档

问题描述

对于我的 Rails 项目,我使用 RSwag 生成使用 OpenAPI 3.0.3 规范的文档。

我知道 Open API 3.0.3 '如果 in 是“header”并且名称字段是“Accept”、“Content-Type”或“Authorization”,则参数定义应被忽略。'

但是我知道它们可以通过正确配置 rspecswagger_helper.rb 文件生成。例如,我的 swagger_helper.rb

  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'
        }
      }
    }
  }

我的一个 rspec 文件

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,type: :string,required: true,example: 'application/vnd.my_company.v1+json'
      parameter name: 'Content-Type',example: 'application/vnd.api+json'
      parameter name: :Authorization,required: true
      parameter name: :id,in: :path,type: :integer,description: 'id of segment'
      parameter name: :marketplace,in: :query,schema: { type: :string },description: 'marketplace of segment'
     context 'abc' do
        response '200',:success do
          run_test! do |response|
           xxxx
          end
        end
     end
end

使用 securitySchemes 的定义并将其与 security [ authorization_header: [] ] 一起使用,以及使用 produces 'application/vnd.my_company.v1+json',我可以发送 AuthorizationAccept 标头我使用“试用”功能的 swagger UI 页面

但是,我无法发送 Content-Type 标头。我哪里做错了?

我知道如果我使用 Swagger 2.0 而不是 OpenAPI 3.0.3 不会出现这个问题,但我不想切换。

更新:

我手动添加

"requestBody": {
          "content": {
            "application/vnd.api+json": {}
          }
        },

下面

  "paths": {
    "/api/segments/{id}": {
      "get": {
        "summary": "Show segment by id","security": [
          {
            "authorization_header": [

            ]
          }
        ],"tags": [
          "segments"
        ],"requestBody": {
          "content": {
            "application/vnd.api+json": {}
          }
        },

在我的 swagger.json 文件中,现在它允许我发送 Content-Type 标头!

但是如何在我的 swagger_helper.rbrspec 文件中执行此操作?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...