[英]Date Time null model schema display swashbuckle + swagger ui
I have implemented Swashbuckle and Swagger UI to do my API documentation.我已经实现了 Swashbuckle 和 Swagger UI 来完成我的 API 文档。
I'm having a problem with correctly displaying a nullable DateTime
, or any nullable object for that matter.我在正确显示可为空的
DateTime
或任何可为空的对象时遇到问题。
Currently a nullable DateTime
displays as "date": "2016-05-27T00:15:41.658Z"
in the response model schema.当前,可为空的
DateTime
在响应模型架构中显示为"date": "2016-05-27T00:15:41.658Z"
。
The response model displays the date as date (string, optional)
which states that it's optional, but it also displays that for a non-nullable DateTime
.响应模型将日期显示为
date (string, optional)
,表明它是可选的,但它也为不可为空的DateTime
显示DateTime
。
I've tried to add default value via: Default model example in Swashbuckle (Swagger) and also tried to add a model schema example via the last answer.我尝试通过以下方式添加默认值: Swashbuckle (Swagger) 中的默认模型示例,并尝试通过最后一个答案添加模型模式示例。
Any ideas on how I can get "date": "null"
to display in the model schema?关于如何在模型模式中显示
"date": "null"
任何想法? Or alternatively find a better way to give the person reading the documentation an indication that the date
object can be null?或者找到一种更好的方法让阅读文档的人指示
date
对象可以为空?
The short answer简短的回答
You can't and you shouldn't你不能也不应该
The slightly longer answer稍长的答案
Basically the documentation provided, date (string, optional)
, is already telling your consumers that the property can be null or missing in a request or a response.基本上提供的文档
date (string, optional)
已经告诉您的消费者该属性可以为空或在请求或响应中丢失。 So, providing an example with "date": null
isn't very helpful.因此,提供一个带有
"date": null
的示例并不是很有帮助。 Providing an example with a valid date, however, is helpful: it gives the consumer an idea of what the date looks like.然而,提供一个包含有效日期的示例是有帮助的:它让消费者了解日期的样子。
Now, on to the DateTime
and DateTime?
现在,转到
DateTime
和DateTime?
properties server side.属性服务器端。 When the server receives an object with a missing date, the type of the property determines the value.
当服务器收到一个缺少日期的对象时,属性的类型决定了它的值。 When a property with type
DateTime
is missing, ASP.Net Web API will set that property to the minimal date (01-01-0001Z00:00:00).当缺少
DateTime
类型的属性时,ASP.Net Web API 会将该属性设置为最小日期 (01-01-0001Z00:00:00)。 However, when a property with type DateTime?
但是,当属性类型为
DateTime?
is missing, ASP.Net Web API will set that property to null.丢失,ASP.Net Web API 会将该属性设置为 null。
The same goes for the response sent by the server.服务器发送的响应也是如此。 When the server sends an object with a property of type
DateTime
it will always send a value.当服务器发送一个具有
DateTime
类型属性的对象时,它总是会发送一个值。 However, when a property with type DateTime?
但是,当属性类型为
DateTime?
is send, the sent property will only have a value when `HasValue' of the property is true.是发送,发送的属性只有在属性的“HasValue”为真时才会有一个值。 Otherwise the response won't have the property.
否则响应将不具有该属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.