【问题标题】:Additional properties not allowed: nullable swagger不允许的附加属性:可为空的招摇
【发布时间】:2018-01-29 15:36:00
【问题描述】:

我正在使用带有 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 字段将为null,并且在删除记录之前不会出现在db 中。 我基于 express 的 nodejs 服务器返回日期如下 -

[{"id":4,"country":"g","created_at":"2018-01-29T04:51:46.000Z","deleted_at":null},{"id":5, "国家":"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":null}]

当我尝试通过 swagger-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 中收到此验证错误

消息:“其他属性不允许:可为空”

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

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

【问题讨论】:

    标签: node.js express swagger swagger-2.0 openapi


    【解决方案1】:

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

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

    【讨论】:

    • 我认为 Swagger 工具不完全支持 OpenApi 3.0。对吗?
    • 有些工具支持它,有些不支持。如果你的意思是swagger-node,那么它还不支持3.0。有一个现有的功能请求github.com/swagger-api/swagger-node/issues/514
    • 暂时我在考虑如果为null就不要发送deleted_at。
    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2023-03-14
    • 2021-12-24
    相关资源
    最近更新 更多