Multiple dock in one project with rswag rails

huangapple go评论92阅读模式
英文:

Multiple dock in one project with rswag rails

问题

我尝试为我的API创建2个文档。我的路由中有2个命名空间,我需要为第一个命名空间和第二个命名空间创建2个文档。在请求测试中,它们分为2个文件夹。如何自动生成这2个文档,以及我需要在路由文件中编写什么?

在路由文件中,我为rswag引擎写了2个挂载点,但我不知道我需要对我的swagger.yml文件做什么。

英文:

I try to create 2 docs for my api. I have 2 namespaces in routes and I need to create 2 docs for my first namespace and second namespace. In request test they are divided on 2 folders. How can I automatically generate this 2 docs and what i need to write in routes file?

In routes I wrote 2 mounts for rswag engine, but I don't not what I need to do with my swagger.yml file

答案1

得分: 0

以下是您要翻译的内容:

Some time later I managed it something like this:

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

And then I use rake rswag it generates for me 2 docs.

请注意,代码部分未被翻译。

英文:

Some time later I managed it something like this:

swagger_helper.rb:

docs = { &#39;v1/robots_api.yaml&#39; =&gt; {
openapi: &#39;3.0.1&#39;,
info: {
title: &#39;API V1&#39;,
version: &#39;v1&#39;
},
paths: {},
components: {
securitySchemes: {
Bearer: {
description: &#39;JWT key necessary to use API calls&#39;,
type: :apiKey,
name: &#39;Authorization&#39;,
in: :header
}
}
},
servers: [
{
url: ENV.fetch(&#39;ROUTES_HOST&#39;),
variables: {
defaultHost: {
default: ENV.fetch(&#39;ROUTES_HOST&#39;)
}
}
}
]

} }

if Rails.env.development?
docs.merge!({ &#39;v1/swagger.yaml&#39; =&gt; {
openapi: &#39;3.0.1&#39;,
info: {
title: &#39;API V1&#39;,
version: &#39;v1&#39;
},
paths: {},
components: {
securitySchemes: {
Bearer: {
description: &#39;JWT key necessary to use API calls&#39;,
type: :apiKey,
name: &#39;Authorization&#39;,
in: :header
}
}
},
servers: [
{
url: ENV.fetch(&#39;ROUTES_HOST&#39;),
variables: {
defaultHost: {
default: ENV.fetch(&#39;ROUTES_HOST&#39;)
}
}
}
]
} })
end
config.swagger_docs = docs

config/initialaziers/rswag_ui.rb

Rswag::Ui.configure do |c|
c.swagger_endpoint &#39;/api-docs/v1/swagger.yaml&#39;, &#39;API V1 Docs&#39; if Rails.env.development?
c.swagger_endpoint &#39;/&lt;api_name&gt;/api-docs/v1/&lt;my_second_doc&gt;.yaml&#39;, &#39;API V1 Docs &lt;my api&gt;&#39;
end

And then I use rake rswag it generates for me 2 docs.

huangapple
  • 本文由 发表于 2023年3月31日 16:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定