2

对于我的 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, 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

通过定义securitySchemes和使用它security [ authorization_header: [] ],以及使用 produces 'application/vnd.my_company.v1+json',我可以使用“试用”功能在我的招摇 UI 页面上发送Authorization和标头。Accept

但是,我无法发送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文件中做到这一点?

谢谢!

4

0 回答 0