describe 'Blogs API' do
path '/blogs' do
post 'Creates a blog' do
response 422, 'invalid request' do
**schema** '$ref' => '#/definitions/errors_object'
...
end
以上代码中的关键字架构对我们有什么好处?真正的反应与之相比吗?如果不匹配,则引发错误?
是的,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' }
}
}
}
}
}