我尝试为我的 api 创建 2 个文档。我在路由中有 2 个命名空间,我需要为我的第一个命名空间和第二个命名空间创建 2 个文档。在请求测试中,它们分为 2 个文件夹。我如何自动生成这 2 个文档以及我需要在路由文件中写什么?
在路线中我为 rswag 引擎写了 2 个安装,但我不知道我需要用我的 swagger.yml 文件做什么
一段时间后,我做了这样的事情:
swagger_helper.rb:
docs = { 'v1/robots_api.yaml' => {
openapi: '3.0.1',
info: {
title: 'API V1',
version: 'v1'
},
paths: {},
components: {
securitySchemes: {
Bearer: {
description: 'JWT key necessary to use API calls',
type: :apiKey,
name: 'Authorization',
in: :header
}
}
},
servers: [
{
url: ENV.fetch('ROUTES_HOST'),
variables: {
defaultHost: {
default: ENV.fetch('ROUTES_HOST')
}
}
}
]
} }
if Rails.env.development?
docs.merge!({ 'v1/swagger.yaml' => {
openapi: '3.0.1',
info: {
title: 'API V1',
version: 'v1'
},
paths: {},
components: {
securitySchemes: {
Bearer: {
description: 'JWT key necessary to use API calls',
type: :apiKey,
name: 'Authorization',
in: :header
}
}
},
servers: [
{
url: ENV.fetch('ROUTES_HOST'),
variables: {
defaultHost: {
default: ENV.fetch('ROUTES_HOST')
}
}
}
]
} })
end
config.swagger_docs = docs
config/initialaziers/rswag_ui.rb
Rswag::Ui.configure do |c|
c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs' if Rails.env.development?
c.swagger_endpoint '/<api_name>/api-docs/v1/<my_second_doc>.yaml', 'API V1 Docs <my api>'
end
然后我使用它为我生成 2 个文档的 rake rswag。