【问题标题】:Swagger-ui connexion not finding Python nested functionsSwagger-ui connexion 找不到 Python 嵌套函数
【发布时间】:2019-05-31 06:40:30
【问题描述】:

我正在尝试使用Swagger 记录一个已经存在的python API。我在editor 的帮助下编写了 swagger.yaml 并记录了每条路线。现在我想使用connexion 部署文档。

(swagger.yaml 文件的简要示例)

swagger: "2.0"
info:
  description: "This is a description of the routes of the API"
  version: "1.0.0"
  title: "API"

basePath: "/api"

paths:
  /home:
    get:
     tags:
      - "API"
      summary: "Home of the application"
      operationId: home
      responses:
        200:
          description: "Success"
          schema:
            type: object
            properties:
              user_id:
                type: string
              username:
                type: string
        403:
          description: "Limit of api connections overrun"

我在服务器启动期间通过 connexion.app 更改了 Flask 应用程序,并且能够指定 .yaml 文件。但是当我尝试启动它时,它会立即崩溃:

  File "/usr/local/lib/python2.7/dist-packages/connexion/utils.py", line 74, in get_function_from_name
raise ValueError("Empty function name")
exceptions.ValueError: Empty function name

据我了解,connexion 将基于对象 operationId 的手动测试功能,在需要指向处理请求的函数的每个路由中。 问题:API 的每个路由都定义为嵌套函数。

def add_routes(app, oauth):
@app.route('/api/home', methods=['GET'])
@oauth.require_oauth()
def home():
    user = request.oauth.user
    return jsonify(
        user_id=user.user_id,
        username=user.username
    )

我知道python中的嵌套函数实际上根本不是函数:不可调用,只是存在于语言中,以便我们程序员组织我们的代码。

我认为这将是 connexion 的问题,它只是无法找到这些功能并将它们映射到手动测试功能,但我不知道如何解决这个问题。您是否看到允许 connexion 映射函数而无需重构整个 API 以便没有嵌套函数的东西?

非常感谢您的帮助。

【问题讨论】:

    标签: python swagger swagger-ui nested-function connexion


    【解决方案1】:

    我的猜测是你还没有定义你的处理函数。您需要提供一个与规范中的 operationId 匹配的模块+函数。

    例如: 我在文件 app.py 中有一个名为 get_user() 的函数,我需要将 operationId 设置为 app.get_user。

    operationId: app.get_user
    

    希望有帮助!

    【讨论】:

    • 你把这个文件放在哪里了?
    • 与 swagger.yaml 文件相同的目录。