Additional properties not allowed: nullable swagge

2019-02-21 01:56发布

I am using swagger 2.0 with node.js express 4.12.3 and mysql db.

I have created following schema -

  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

Here deleted_at field will be null and will not be present in db until the record is deleted. My express based nodejs server returns the date as following -

[{"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":null}]

When I try to make rest call via swagger-ui I get following error-

"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"},

After reading the docs I did following-

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

Then I started getting this validation error in swagger-ui

message: "Additional properties not allowed: nullable"

I tried setting type to object from string but even that did not worked.

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

1条回答
走好不送
2楼-- · 2019-02-21 02:42

OpenAPI/Swagger 2.0 does not allow nullable values for any types. nullable: true was introduced in OpenAPI 3.0, it's not supported in OpenAPI/Swagger 2.0.

The workaround is to not send deleted_at if it's supposed to be null.

查看更多
登录 后发表回答