quart-openapi
quart-openapi copied to clipboard
auto-generated openapi doc wrong url in swagger ui
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 ?
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
}
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}]