首页 > 解决方案 > Flask/Flasgger - 如果设置了 `endpoint` 参数,文档不会出现

问题描述

我有一个蓝图,我为其编写了 OpenAPI 文档。没有端点定义,它工作得很好,但它没有端点定义。

工作代码:

@my_blueprint.route('/')
@swag_from('open_api/root.yml')
def main():
    return str('This is the root api')

不工作(注意我如何在参数中定义端点):

@my_blueprint.route('/', endpoint='foo')
@swag_from('open_api/root.yml', endpoint='foo')
def main():
    return str('This is the root api')

你有工作代码,你为什么要问?

对我来说,用例是当我只有一个函数的多端点时,我必须yml为每个文档定义多个文件。

@my_blueprint.route('/', endpoint='foo')
@my_blueprint.route('/<some_id>', endpoint='foo_with_id')
@swag_from('open_api/root.yml', endpoint='foo')
@swag_from('open_api/root_with_id.yml', endpoint='foo_with_id')
def main(some_id):
    if (some_id):
        return str('Here's your ID')

    return str('This is the root api')

标签: python-3.xflaskflasgger

解决方案


设置端点@swag_from还应包含蓝图的名称。例子:@swag_from('my_yaml.yml', endpoint='{}.your_endpoint'.format(my_blueprint.name))

完整示例:

@my_blueprint.route('/', endpoint='foo') # endpoint is foo
@my_blueprint.route('/<some_id>', endpoint='foo_with_id') # endpoint is foo_with_id
@swag_from('open_api/root.yml', endpoint='{}.foo'.format(my_blueprint.name)) # blueprint is set as the prefix for the endpoint
@swag_from('open_api/root_with_id.yml', endpoint='{}.foo_with_id'.format(my_blueprint.name)) # same goes here
def main(some_id):
    if (some_id):
        return str("Here's your ID")

    return str('This is the root api')

推荐阅读