不允许使用其他属性:nullable swagger

时间:2018-01-29 15:36:00

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

我使用swagger 2.0与node.js表达4.12.3和mysql db。

我创建了以下架构 -

  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&#34:4,"国家":" G"" created_at":" 2018-01 -29T04:51:46.0​​00Z"" deleted_at":空},{" ID":5,"国家":" gaaaf&# 34;," created_at":" 2018-01-29T04:54:59.000Z"" deleted_at":空},{" ID&#34 ; 6"国家":" ABCD"" created_at":" 2018-01-29T04:57:02.000Z",& #34; deleted_at":空}]

当我尝试通过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"

1 个答案:

答案 0 :(得分:1)

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

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