Multiple dock in one project with rswag rails

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

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

以下是您要翻译的内容:

  1. Some time later I managed it something like this:
  2. swagger_helper.rb:
  3. docs = { 'v1/robots_api.yaml' => {
  4. openapi: '3.0.1',
  5. info: {
  6. title: 'API V1',
  7. version: 'v1'
  8. },
  9. paths: {},
  10. components: {
  11. securitySchemes: {
  12. Bearer: {
  13. description: 'JWT key necessary to use API calls',
  14. type: :apiKey,
  15. name: 'Authorization',
  16. in: :header
  17. }
  18. }
  19. },
  20. servers: [
  21. {
  22. url: ENV.fetch('ROUTES_HOST'),
  23. variables: {
  24. defaultHost: {
  25. default: ENV.fetch('ROUTES_HOST')
  26. }
  27. }
  28. }
  29. ] }
  30. if Rails.env.development?
  31. docs.merge!({ 'v1/swagger.yaml' => {
  32. openapi: '3.0.1',
  33. info: {
  34. title: 'API V1',
  35. version: 'v1'
  36. },
  37. paths: {},
  38. components: {
  39. securitySchemes: {
  40. Bearer: {
  41. description: 'JWT key necessary to use API calls',
  42. type: :apiKey,
  43. name: 'Authorization',
  44. in: :header
  45. }
  46. }
  47. },
  48. servers: [
  49. {
  50. url: ENV.fetch('ROUTES_HOST'),
  51. variables: {
  52. defaultHost: {
  53. default: ENV.fetch('ROUTES_HOST')
  54. }
  55. }
  56. }
  57. ] })
  58. end
  59. config.swagger_docs = docs
  60. config/initialaziers/rswag_ui.rb
  61. Rswag::Ui.configure do |c|
  62. c.swagger_endpoint '/api-docs/v1/swagger.yaml', 'API V1 Docs' if Rails.env.development?
  63. c.swagger_endpoint '/<api_name>/api-docs/v1/<my_second_doc>.yaml', 'API V1 Docs <my api>'
  64. end
  65. And then I use rake rswag it generates for me 2 docs.

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

英文:

Some time later I managed it something like this:

swagger_helper.rb:

  1. docs = { &#39;v1/robots_api.yaml&#39; =&gt; {
  2. openapi: &#39;3.0.1&#39;,
  3. info: {
  4. title: &#39;API V1&#39;,
  5. version: &#39;v1&#39;
  6. },
  7. paths: {},
  8. components: {
  9. securitySchemes: {
  10. Bearer: {
  11. description: &#39;JWT key necessary to use API calls&#39;,
  12. type: :apiKey,
  13. name: &#39;Authorization&#39;,
  14. in: :header
  15. }
  16. }
  17. },
  18. servers: [
  19. {
  20. url: ENV.fetch(&#39;ROUTES_HOST&#39;),
  21. variables: {
  22. defaultHost: {
  23. default: ENV.fetch(&#39;ROUTES_HOST&#39;)
  24. }
  25. }
  26. }
  27. ]

} }

  1. if Rails.env.development?
  2. docs.merge!({ &#39;v1/swagger.yaml&#39; =&gt; {
  3. openapi: &#39;3.0.1&#39;,
  4. info: {
  5. title: &#39;API V1&#39;,
  6. version: &#39;v1&#39;
  7. },
  8. paths: {},
  9. components: {
  10. securitySchemes: {
  11. Bearer: {
  12. description: &#39;JWT key necessary to use API calls&#39;,
  13. type: :apiKey,
  14. name: &#39;Authorization&#39;,
  15. in: :header
  16. }
  17. }
  18. },
  19. servers: [
  20. {
  21. url: ENV.fetch(&#39;ROUTES_HOST&#39;),
  22. variables: {
  23. defaultHost: {
  24. default: ENV.fetch(&#39;ROUTES_HOST&#39;)
  25. }
  26. }
  27. }
  28. ]
  29. } })
  30. end
  31. config.swagger_docs = docs

config/initialaziers/rswag_ui.rb

  1. Rswag::Ui.configure do |c|
  2. c.swagger_endpoint &#39;/api-docs/v1/swagger.yaml&#39;, &#39;API V1 Docs&#39; if Rails.env.development?
  3. 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;
  4. 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:

确定