ruby-on-railsrubyrspecdocumentationrswag

Multiple dock in one project with rswag rails


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


Solution

  • 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.