问题描述
我正在使用带有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
此处的delete_at字段将为null,直到删除记录后,该字段才会出现在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,国家":"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}]
[{"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.
解决方法是如果应该发送null
,则不发送deleted_at
.
The workaround is to not send deleted_at
if it's supposed to be null
.
这篇关于不允许其他属性:可为空的招摇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!