【问题标题】:(Swagger 2.0/ Connexion) None type is not of type 'string'(Swagger 2.0/ Connexion)无类型不是“字符串”类型
【发布时间】:2020-01-03 03:20:16
【问题描述】:

我正在尝试通过 python connexion 创建一个简单的帖子 API。这是我的 api yaml 文件:

swagger: "2.0"

info:
  title: "My first API"
  version: "1.0"

basePath: /v1.0

paths:
  /potential:
    post:
      operationId: api.data.create_potential_data
      summary: Create the potential data
      parameters:
        - name: alertCategory
          in: body
          required: True
          description: The potential API request AlertCategoryDto object with settings of alert category (categorySystemName, alert Kpis etc.)
          schema:
            $ref: "#/definitions/alert"
      responses:
        201:
          description: Successfully created potential data


definitions:
  alert:
    properties:
      systemName:
        type: string
      name:
        type: string
      moduleSystemName:
        type: string

但是对于name 属性之一,我们允许用户输入null 值。如果他们真的这样做,我们将得到error: None type is not of type 'string'

那么在 swagger 2.0 中是否有让属性可以为空的方法?网上找不到太多资料。谢谢!

【问题讨论】:

标签: python flask openapi connexion


【解决方案1】:

我认为你应该写下 type: string 可为空:真

从这里我得到了信息 This link

应该是这样的:

definitions:
  alert:
    properties:
      systemName:
        type: string
      name:
        type: string
        nullable: true
      moduleSystemName:
        type: string

【讨论】:

  • 感谢您的回复达尼洛!唯一的问题是 nullable 函数仅适用于 openAPI 3.0,而 python connexion 仍在使用 openAPI 2.0
  • 尝试类型:[string, null] 不确定它是否有效。在此处获取信息:github.com/zalando/connexion/issues/226 并且该人写道它有效
  • 谢谢。我终于从 connexion 的文档中找到了答案
【解决方案2】:

感谢@Danilo 的帮助,我在此处从 connexion 的文档中找到了答案:

总之,添加x-nullable = true 即可解决此问题。

【讨论】:

    猜你喜欢
    • 2019-11-21
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多