不允许的附加属性:可为空的招摇 [英] Additional properties not allowed: nullable swagger
问题描述
我正在使用带有 node.js express 4.12.3 和 mysql db 的 swagger 2.0.
I am using swagger 2.0 with node.js express 4.12.3 and 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 字段将为空,并且在删除记录之前不会出现在db 中.我基于 express 的 nodejs 服务器返回日期如下 -
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,"国家":"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}]
[{"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}]
当我尝试通过 swagger-ui 进行休息调用时,出现以下错误-
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"
然后我开始在 swagger-ui 中收到此验证错误
Then I started getting this validation error in swagger-ui
消息:其他属性不允许:可为空"
我尝试将类型设置为字符串中的对象,但即使这样也没有用.
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"
推荐答案
OpenAPI/Swagger 2.0 不允许 nullable: true
在 OpenAPI 3.0 中引入,在 OpenAPI/Swagger 2.0 中不支持.
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.
解决方法是不发送 deleted_at
如果它应该是 null
.
The workaround is to not send deleted_at
if it's supposed to be null
.
这篇关于不允许的附加属性:可为空的招摇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!