I have a Flask / Flask-Smorest application which when deployed locally the swagger UI and Open API show correctly. However, when deployed on the Cloud onto Nginx
then it produces the output:
Unable to render this definition
The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: "3.x.y" (for example, openapi: 3.1.0)
I have tried with two different approaches.
==================== FIRST APPROACH ====================
The first approach is a copy paste of the Flask-Smorest documentation OpenAPI Flask-Smorest:
class MyConfig:
OPENAPI_VERSION = '3.0.3'
OPENAPI_URL_PREFIX = '/docs'
OPENAPI_SWAGGER_UI_PATH = ''
# I tried with each of the possibilities given
OPENAPI_SWAGGER_UI_URL = 'https://cdn.jsdelivr.net/npm/swagger-ui-dist/'
I even ensure that the endpoint is covered by CORS to avoid any issues there:
from flask import Flask
from flask_cors import CORS
from flask_smorest import Api
app = Flask(__name__)
CORS(app, resources={'/api/*': {'origins': '*'}, '/api/*': {'docs': '*'}})
api = Api(app)
cache.init_app(app)
==================== SECOND APPROACH ====================
I downloaded and extracted Swagger UI latest v5.9.0 under the source folder static\swagger-ui
. Then changed the application from above in the following ways:
class MyConfig:
OPENAPI_VERSION = '3.0.3'
OPENAPI_URL_PREFIX = '/docs'
OPENAPI_SWAGGER_UI_PATH = ''
OPENAPI_SWAGGER_UI_URL = '/static/swagger-ui'
@app.route('static/<path:path>')
def serve_static(path):
return send_from_directory('static', path)
Both approaches work perfectly locally, the later approach showing a much fancier Swagger interface. But both approaches fail on the Nginx server with the same error. I then inspected the swagger json file referenced by swagger-initializer.js
:
https://petstore.swagger.io/v2/swagger.json
which indeed does include the version in the first line: "swagger":"2.0"
so I don't understand why I'm getting this error.