如何使用Flagsgger与Flask应用程序使用蓝图?

我使用
Flasgger将Swagger UI添加到我的Python Flask应用程序中.互联网上最常见的例子是使用@ app.route的基本Flask风格:

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'])
....

如上所示,我在TestResourceClass.get()方法上使用@swag_from装饰器,该方法绑定到GET方法端点.我在两个地方也有端点=测试匹配.

但是我在Swagger UI上没有得到任何东西,它都是空白的. docs_test_get.yml文件包含有效的yaml标记以定义swagger规范.

我错过了什么?如何让Flasgger Swagger UI与基于Flask Blueprint的设置一起使用?

最佳答案 现在有一个
https://github.com/rochacbruno/flasgger/blob/master/examples/example_blueprint.py的蓝图应用程序示例

"""
A test to ensure routes from Blueprints are swagged as expected.
"""
from flask import Blueprint, Flask, jsonify

from flasgger import Swagger
from flasgger.utils import swag_from

app = Flask(__name__)

example_blueprint = Blueprint("example_blueprint", __name__)


@example_blueprint.route('/usernames/<username>', methods=['GET', 'POST'])
@swag_from('username_specs.yml', methods=['GET'])
@swag_from('username_specs.yml', methods=['POST'])
def usernames(username):
    return jsonify({'username': username})


@example_blueprint.route('/usernames2/<username>', methods=['GET', 'POST'])
def usernames2(username):
    """
    This is the summary defined in yaml file
    First line is the summary
    All following lines until the hyphens is added to description
    the format of the first lines until 3 hyphens will be not yaml compliant
    but everything below the 3 hyphens should be.
    ---
    tags:
      - users
    parameters:
      - in: path
        name: username
        type: string
        required: true
    responses:
      200:
        description: A single user item
        schema:
          id: rec_username
          properties:
            username:
              type: string
              description: The name of the user
              default: 'steve-harris'
    """
    return jsonify({'username': username})


app.register_blueprint(example_blueprint)

swag = Swagger(app)

if __name__ == "__main__":
    app.run(debug=True)
点赞