Swagger-ui连接找不到Python嵌套函数

时间:2019-01-04 17:03:05

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

我正在尝试使用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中的嵌套函数实际上根本不是函数:不可调用,只是以该语言显示,以便我们程序员组织我们的代码。

我认为这将是连接的问题,它只是无法找到这些功能并为手动测试功能映射它们,但是我不确定如何解决此问题。您是否看到一些可以让连接映射功能而不必重构整个API而不具有嵌套功能的东西?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我的猜测是您尚未定义处理程序函数。您需要提供与规范中的operationId匹配的模块+功能。

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

operationId: app.get_user

希望有帮助!