是否可以使用 flask-RESTplus 自动生成 swagger 元数据 json?

is it possible to generate swagger metadata json automatically with flask-RESTplus?

我使用 python 库 flask-Restplus 创建了一个 api,并将其关联的 swagger ui 暴露给 http://serverURL:80/api/v1/documentation

是否可以通过某种方式将此文档页面公开为 json? 我知道这可以使用 .Net 库 "Swashbuckle"(例如 http://serverURL:80/api/v1/documentation.json 将 return json 格式的文档)。我正在 python.

中寻找实现此类功能的方法

原来swaggerui的这个.json并不是在文档页面生成的,而是自动生成了带有flask_Restplus库的根目录。例如“http://serverURL:80/api/v1/swagger.json" was the url which had this page instead of my documentation url "http://serverURL:80/api/v1/documentation”。我不需要做任何代码更改就可以工作。

作为 运行 开发服务器和打开浏览器的替代方法,这里有一些代码可以将 Swagger 规范提取为 JSON:

from flask import Flask
from flask_restplus import Api

app = Flask(__name__)
api = Api(app)

def print_swagger_file():
    with app.app_context():
        print(json.dumps(api.__schema__, indent=4))

要获得 swagger.json,请转到 :/swagger.json 您将获得 swagger.json。使用此 swagger.json 生成文档。 转到 https://editor.swagger.io 并添加你的招摇。