首页 > 解决方案 > 请求失败,因为缺少“default_body”

问题描述

我使用 swagger-codegen 来生成 Flask-Connexion 服务器存根。这通常很好用,但我的 POST 方法失败了。检查代码显示,在 中openapi._get_body_argument()default_body是一个空字典,导致行中的异常

body_arg = deepcopy(default_body)
body_arg.update(body or {})

更新调用失败并显示“TypeError:无法将字典更新序列元素#0 转换为序列”,因为default_bodybody_arg是空的)。

这是什么default_body?我该怎么做才能确保正确填充?

一些元数据:我正在使用 swagger-codegen V3.0.29、Connexion V2.6(行为与 V2.9 相同)、Flask V1.1.4 和 Werkzeug V1.0.1。可以通过为标准“宠物商店”示例生成代码来观察该行为。

架构:

components:
  schemas:
    Tag:
      type: object
      properties:
        tag_id:
          type: string
        name:
          type: string
        parent_id:
          type: string
      example:
        tag_id: "42"
        name: "My tag"
        parent_id: "2"

操作定义:

paths:
  /tag:
    post:
      tags:
      - tag
      summary: Add a new tag
      operationId: add_tag
      requestBody:
        description: The new tag
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Tag'
          application/xml:
            schema:
              $ref: '#/components/schemas/Tag'
        required: true
      responses:
        "405":
          description: Invalid input
          content: {}
      security:
        - BasicAuth: []
      x-codegen-request-body-name: body
      x-openapi-router-controller: swagger_server.controllers.tag_controller

标签: pythonswagger-codegenconnexion

解决方案


推荐阅读