Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto-generated openapi doc wrong url in swagger ui #55

Open
YievCkim opened this issue Sep 26, 2021 · 2 comments
Open

auto-generated openapi doc wrong url in swagger ui #55

YievCkim opened this issue Sep 26, 2021 · 2 comments

Comments

@YievCkim
Copy link

YievCkim commented Sep 26, 2021

Hi,

Here my openapi schema:

{
    "openapi": "3.0.0",
    "info": {
        "title": "OpenApi Rest Documentation",
        "version": "1.0"
    },
    "servers": [
        {
            "url": "http://"
        }
    ],
    "paths": {
        "/trolls": {
            "get": {
                "summary": "Retrieve trolls or specific troll",
                "description": "",
                "tags": [],
                "responses": {
                    "200": {
                        "description": "Success"
                    }
                },
                "operationId": "get_troll"
            }
        }
    }
}

I added a html template file to serve swagger-ui in my project:

<!Doctype html>
<html>
  <head>
    <title>Swagger</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" />
  </head>
  <body>
    <h1>{{ url_for ('openapi') }}</h1>
    <div id="swagger-ui"></div>
    <script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
    <script>
      const ui = SwaggerUIBundle({
          url: "{{ url_for ('openapi') }}",
          dom_id: '#swagger-ui',
          presets: [
              SwaggerUIBundle.presets.apis,
              SwaggerUIBundle.SwaggerUIStandalonePreset
          ],
          layout: "BaseLayout",
          requestInterceptor: (request) => {
              request.headers['X-CSRFToken'] = "{{ csrf_token }}"
              return request;
          }
      })
    </script>
  </body>
</html>

Everything work as expected but "try it out" button doesn't work because it try to perform request against http://mydomain/openapi.json/trolls instead of http://mydomain/trolls as expected.

I tried to find in swagger-ui documentation how to specify the base_url. But i didn't find something to do that. I am not sure my issue related to quart-openapi or a misconfiguration on swagger-ui.html.

Is there something to specify the base_url to test for swagger-ui ? Is there an information I have to provice in openapi.json or in SwaggerUIBundle ?

@YievCkim
Copy link
Author

YievCkim commented Sep 26, 2021

I looked into swagger.py and I propose to do that instead at l-143:

from quant import request

# ...

        spec = {
            'openapi': '3.0.0',
            'info': infos,
            'servers': [
                {
                    'url': request.root_url or ''.join([scheme, '://', self.api.config['SERVER_NAME'] or ''])
                }
            ],
            'paths': paths,
            'components': components
        }

@isomc-stone
Copy link

This is an annoying bug, @MikHulk 's solution should work

For now, here is my workaround

@app.before_first_request
async def setup():
    app.__schema__   # ensure _schema property is set
    app._schema['servers'] = [{'url': request.root_url}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants