简体   繁体   English

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

[英]Swagger-ui connexion not finding Python nested functions

I'm trying to document an already existing python API with Swagger . 我正在尝试使用Swagger记录现有的python API。 I wrote the swagger.yaml with every route documented with the help of their editor . 我在编辑器的帮助下编写了swagger.yaml,并记录了每条路线。 Now i would like to deploy the documentation using connexion . 现在,我想使用connexion部署文档。

(brief example of the swagger.yaml file) (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"

I changed the Flask app by a connexion.app during the launch of the server, and was able to specify the .yaml file. 在服务器启动期间,我通过connexion.app更改了Flask应用程序,并且能够指定.yaml文件。 But when i'm trying to launch it, it crashes instantly: 但是,当我尝试启动它时,它立即崩溃:

  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

From my understanding connexion will base it's manual testing feature from the object operationId in every route that needs to point on the function handling the request. 根据我的理解,connexion将基于对象operationId的手动测试功能,该功能需要指向处理请求的函数的每条路径。 Problem: every route of the API are defined as nested function. 问题: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
    )

I know nested functions in python are not actually functions at all: not callable, just present in the language in order for us programmers to organize our code. 我知道python中的嵌套函数实际上根本不是函数:不可调用,只是以该语言显示,以便我们程序员组织我们的代码。

I think that would be the issue with connexion, it is just not capable of finding these functions and map them for the manual testing feature, but i'm not sure how to fix this. 我认为这将是连接的问题,它只是无法找到这些功能并为手动测试功能映射它们,但我不确定如何解决此问题。 Do you see something that would allow connexion to map the function without having to refactor the entire API in order not to have nested functions ? 您是否看到允许连接连接映射功能而不必重构整个API而不具有嵌套功能的内容?

Thanks a lot for any help. 非常感谢您的帮助。

My guess is that you haven't defined your handler function. 我的猜测是您尚未定义处理程序函数。 You need to provide a module+function that matches the operationId in the spec. 您需要提供与规范中的operationId匹配的module + function。

For example: I have a function called get_user() in a file app.py , I need to set the operationId to app.get_user. 例如:我在文件app.py中有一个名为get_user()的函数,我需要将operationId设置为app.get_user。

operationId: app.get_user

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM