不允许的其他属性:可为空的 swagger

Additional properties not allowed: nullable swagger

我正在使用带有 node.js express 4.12.3 和 mysql db 的 swagger 2.0。

我创建了以下架构 -

  Country:
    type: "object"
    properties:
      id:
        type: "integer"
        readOnly: true
        description: "Country Id"
      country:
        type: "string"
        description: "Country name"
      created_at:
        type: "string"
        readOnly: true
        format: "date-time"
        description: "Country record creation date"
      deleted_at:
        type: "string"
        format: "date-time"
        description: "Country record delete date"
    required:
      - country

此处 deleted_at 字段将为空,并且在删除记录之前不会出现在数据库中。 我基于 express 的 nodejs 服务器 returns 日期如下 -

[{"id":4,"country":"g","created_at":"2018-01-29T04:51:46.000Z","deleted_at":null},{"id":5,"country":"gaaaf","created_at":"2018-01-29T04:54:59.000Z","deleted_at" :null},{"id":6,"country":"abcd","created_at":"2018-01-29T04:57:02.000Z","deleted_at":空}]

当我尝试通过 swagger 进行 rest 调用时-ui 我收到以下错误-

"message":"Response validation failed: failed schema

validation","code":"SCHEMA_VALIDATION_FAILED","failedValidation":true,
{"errors":[{"code":"INVALID_TYPE","message":"Expected type string but found type null","path":["5","deleted_at"],"description":"Country record delete date"},

阅读文档后我做了以下操作-

  deleted_at:
    type: "string"
    format: "date-time"
    nullable: true
    description: "Country record delete date"

然后我开始在 swagger 中收到这个验证错误-ui

留言:"Additional properties not allowed: nullable"

我尝试将类型设置为字符串中的对象,但即使那样也没有用。

 deleted_at:
        type: "object"
        format: "date-time"
        nullable: true
        description: "Country record delete date"

OpenAPI/Swagger 2.0 nullable values 适用于任何类型。 nullable: true 在 OpenAPI 3.0 中引入,在 OpenAPI/Swagger 2.0 中不受支持。

解决方法是不发送 deleted_at,如果它应该是 null