OpenAPI 3.0 with flask, unable to view UI

Viewed 262

I want to generate openAPI specs for the python application. I am able to generate swagger.json

import logging
import json
from apispec import APISpec
from init import get_slack_url
from flask import Flask
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
logging.basicConfig(level=logging.DEBUG)
hook_url = get_slack_url()
spec = APISpec(
    title="Notification Service",
    version="1.0.0",
    openapi_version="3.0.2",
    plugins=[FlaskPlugin(), MarshmallowPlugin()]
)

app = Flask(__name__)
@app.route('/slack_notification', methods=['POST'])
def send_slack_notification():
    return 'wathever'


with app.test_request_context():
    spec.path(view=send_slack_notification)
with open('swagger.json', 'w') as f:
    json.dump(spec.to_dict(), f)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001, debug=False)

swagger.json is generated.

{"paths": {"/slack_notification": {}}, "info": {"title": "Notification Service", "version": "1.0.0"}, "openapi": "3.0.2"}

But I am not able to load swagger or apidocs endpoint. I get the error.

INFO:werkzeug: * Running on http://0.0.0.0:5001/ (Press CTRL+C to quit)
INFO:werkzeug:127.0.0.1 - - [27/Oct/2021 21:03:50] "GET /swagger/ HTTP/1.1" 404 -
INFO:werkzeug:127.0.0.1 - - [27/Oct/2021 21:03:53] "GET /openapi HTTP/1.1" 404 -
INFO:werkzeug:127.0.0.1 - - [27/Oct/2021 21:03:57] "GET / HTTP/1.1" 404 -

Any suggestions on how do I see this on the UI?

The other alternative I tried is Flasgger. I could see the UI, but the specs had swagger: 2.0, and I want specifically OpenApi 3.0.

Reference: https://donofden.com/blog/2020/06/14/Python-Flask-automatically-generated-Swagger-3-0-openapi-Document

0 Answers
Related