2

使用 gem rswag-specs (2.3.1)。根据https://swagger.io/docs/specification/components/定义:responses_components

 config.swagger_docs = {
  'swagger.yaml' => {
      openapi: '3.0.1',
      info: {
        title: 'title',
      },
      paths: {},
      components: {
        responses: {
          "403": {
            type: :object,
            properties: {
              errors: {
                type: :array,
                items: {
                  '$ref' => '#/components/schemas/forbidden_error'
                }
              }
            }
          }
        }
      }
    }
  }

测试失败并出现错误:

      response '403', 'Wrong permissions' do
        let(:Authorization) { "Bearer #{token}" }
        let(:id) { item.id }
        schema '$ref' => '#/components/responses/403'

        run_test!
      end
 JSON::Schema::SchemaError:
       The fragment '/components/responses' does not exist on schema aec776a1-291e-5317-89bf-6ea163b4bac3

如何使用 /components/responses 部分?

4

1 回答 1

0

更新

我的例子'#/components/schemas/forbidden_error'没有定义。尝试将其添加到/components/schemas或内联它,如"404"示例中所示。

config.swagger_docs = {
  'swagger.yaml' => {
    openapi: '3.0.1',
    info: {
      title: 'title',
    },
    paths: {},
    components: {
      schemas: {
        forbidden_error: {
          type: object,
          properties: {
            message: string
          }
        }
      },  
      responses: {
        "403": {
          type: :object,
          properties: {
            errors: {
              type: :array,
              items: {
                '$ref' => '#/components/schemas/forbidden_error'
              }
            }
          }
        },
        "404": {
          type: :object,
          properties: {
            errors: {
              type: :array,
              items: {
                type: object,
                properties: {
                  message: string
                }
              }
            }
          }
        }
      }
    }
  }
}

原始答案

这是一种错误的做法。

rswag gem 自述文件中的示例表明嵌套是components/schemas/refrenced_object. 在您的示例responses中,位于components. responses也会在 `schemas 下移动。

于 2020-10-28T19:57:49.617 回答