1

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"
WitVault
  • 23,445
  • 19
  • 103
  • 133

1 Answers1

1

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.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • I think OpenApi 3.0 is not fully supported by swagger tools. right? – WitVault Jan 29 '18 at 16:51
  • Some tools support it, some do not. If you mean [swagger-node](https://github.com/swagger-api/swagger-node/), then it does not support 3.0 yet. There's an existing feature request https://github.com/swagger-api/swagger-node/issues/514. – Helen Jan 29 '18 at 16:56
  • For the time being I am thinking about not sending deleted_at if it is null. – WitVault Jan 29 '18 at 18:18