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

问题描述 投票:2回答:1
describe 'Blogs API' do

  path '/blogs' do

    post 'Creates a blog' do

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

以上代码中的关键字架构对我们有什么好处?真正的反应与之相比吗?如果不匹配,则引发错误?

ruby-on-rails rspec ruby-on-rails-5 rswag
1个回答
0
投票

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

在您给出的示例中,它将在errors_object中查找config.swagger_docs。 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' }
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.