简体   繁体   English

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

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

I have created an api using the python library flask-Restplus and have it's associated swagger ui being exposed to eg http://serverURL:80/api/v1/documentation .我已经使用 python 库flask-Restplus 创建了一个api,并将其关联的swagger ui 暴露给例如http://serverURL:80/api/v1/documentation

Is it possible to expose this documentation page as a json somehow?是否可以以某种方式将此文档页面公开为 json? I know this is possible using the .Net library "Swashbuckle" (for eg http://serverURL:80/api/v1/documentation.json would return the documentation in json format).我知道这可以使用 .Net 库“Swashbuckle”(例如http://serverURL:80/api/v1/documentation.json将返回 json 格式的文档)。 I am looking for a way for this same type of functionality in python.我正在寻找一种在 python 中实现这种相同类型功能的方法。

It turns out that this .json of the swagger ui is not generated in the documentation page but rather automatically generated the root directory with flask_Restplus library.事实证明,swagger ui 的这个 .json 不是在文档页面中生成的,而是使用 flask_Restplus 库自动生成的根目录。 for example " 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 ".例如“ http://serverURL:80/api/v1/swagger.json ”是这个页面的url,而不是我的文档url“ http://serverURL:80/api/v1/documentation ”。 I didn't have to make any code changes for this to work.我不必进行任何代码更改即可使其正常工作。

As an alternative to running the development server and opening the browser, here is some code to extract the Swagger specification as JSON:作为运行开发服务器并打开浏览器的替代方法,以下是一些将 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))

To get the swagger.json, go to the :/swagger.json you will get the swagger.json.要获取 swagger.json,请转到 :/swagger.json,您将获得 swagger.json。 Use this swagger.json to generate the documents .使用此 swagger.json 生成文档。 Go to https://editor.swagger.io and add ur swagger.转到https://editor.swagger.io并添加您的 swagger。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM