Im trying to generate swagger document for my existing Flask app, I tried with Flask-RESTPlus
initially and found out the project is abundant now and checked at the forked project flask-restx
https://github.com/python-restx/flask-restx but still i dont think they support openapi 3.0
Im a bit confused to choose the package for my need. Im looking to solve a problem where we dont want to manually create swagger doc for our API, instead we would like to generate automatically using a packages.
import os
import requests
import json, yaml
from flask import Flask, after_this_request, send_file, safe_join, abort
from flask_restx import Resource, Api, fields
from flask_restx.api import Swagger
app = Flask(__name__)
api = Api(app=app, doc='/docs', version='1.0.0-oas3', title='TEST APP API',
description='TEST APP API')
response_fields = api.model('Resource', {
'value': fields.String(required=True, min_length=1, max_length=200, description='Book title')
})
@api.route('/compiler/', endpoint='compiler')
# @api.doc(params={'id': 'An ID'})
@api.doc(responses={403: 'Not Authorized'})
@api.doc(responses={402: 'Not Authorized'})
# @api.doc(responses={200: 'Not Authorized'})
class DemoList(Resource):
@api.expect(response_fields, validate=True)
@api.marshal_with(response_fields, code=200)
def post(self):
"""
returns a list of conferences
"""
api.payload["value"] = 'Im the response ur waiting for'
return api.payload
@api.route('/swagger')
class HelloWorld(Resource):
def get(self):
data = json.loads(json.dumps(api.__schema__))
with open('yamldoc.yml', 'w') as yamlf:
yaml.dump(data, yamlf, allow_unicode=True, default_flow_style=False)
file = os.path.abspath(os.getcwd())
try:
@after_this_request
def remove_file(resp):
try:
os.remove(safe_join(file, 'yamldoc.yml'))
except Exception as error:
log.error("Error removing or closing downloaded file handle", error)
return resp
return send_file(safe_join(file, 'yamldoc.yml'), as_attachment=True, attachment_filename='yamldoc.yml', mimetype='application/x-yaml')
except FileExistsError:
abort(404)
# main driver function
if __name__ == '__main__':
app.run(port=5003, debug=True)
The above code is a combination of my try on different packages, but it can generate swagger 2.0 doc but im trying to generate doc for openapi 3.0
Can some one suggest a good package which is supporting openapi 3.0 way of generating swagger yaml or json.