我正在使用以下命令将 Swagger UI 添加到我的 Python Flask 应用程序中Flasgger https://github.com/rochacbruno/flasgger。互联网上最常见的示例是使用基本 Flask 风格@app.route
:
from flasgger.utils import swag_from
@app.route('/api/<string:username>')
@swag_from('path/to/external_file.yml')
def get(username):
return jsonify({'username': username})
这样可行。
然而,在我的应用程序中,我没有使用@app.route
装饰器来定义端点。我正在使用烧瓶蓝图。就像下面这样:
from flask import Flask, Blueprint
from flask_restful import Api, Resource
from flasgger.utils import swag_from
...
class TestResourceClass(Resource):
@swag_from('docs_test_get.yml', endpoint='test')
def get() :
print "This is the get method for GET /1.0/myapi/test endpoint"
app = Flask(__name__)
my_api_blueprint = Blueprint('my_api', __name__)
my_api = Api(my_api_blueprint)
app.register_blueprint(my_api_blueprint, url_prefix='/1.0/myapi/')
my_api.add_resource(TestResourceClass, '/test/'
endpoint='test',
methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])
....
如上所示,我使用了@swag_from
装饰器上的TestResourceClass.get()
绑定到 GET 方法端点的方法。我也有端点=test
在两个地方进行匹配。
但我在 Swagger UI 上没有得到任何信息,它都是空白的。这docs_test_get.yml
文件确实包含定义 swagger 规范的有效 yaml 标记。
我缺少什么?如何让 Flasgger Swagger UI 与基于 Flask 蓝图的设置一起使用?