rswag gem:响应块中模式的主要目的是什么?

rswag gem: what is the main purpose of schema in the response block?

describe 'Blogs API' do

  path '/blogs' do

    post 'Creates a blog' do

      response 422, 'invalid request' do
        **schema** '$ref' => '#/definitions/errors_object'
  ...
end

上面代码中的keyword schema对我们有什么好处? 真正的反应能与之相比吗?如果不匹配,则引发错误?

是的,rswag 将验证您的 API 响应是否与您在 运行 测试时指定的模式匹配。它还会在生成的 swagger 文档中将架构输出为 'Model'。

在您提供的示例中,它将在您的 config.swagger_docs 中查找 errors_object。 rswag 文档向您展示了如何定义它:

config.swagger_docs = {
  'v1/swagger.json' => {
    swagger: '2.0',
    info: {
      title: 'API V1'
    },
    definitions: {
      errors_object: {
        type: 'object',
        properties: {
          errors: { '$ref' => '#/definitions/errors_map' }
        }
      },
      errors_map: {
        type: 'object',
        additionalProperties: {
          type: 'array',
          items: { type: 'string' }
        }
      }
    }
  }
}