简体   繁体   English

日期时间空模型架构显示 swashbuckle + swagger ui

[英]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?现在,转到DateTimeDateTime? 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.

相关问题 如何解决 generics 的 Swashbuckle/Swagger -UI model 命名问题? - How to resolve Swashbuckle/Swagger -UI model naming issue for generics? 在 Swagger UI 中,如何自定义 body 输入 ui(模型架构 UI) - In Swagger UI, how to customize body input ui (model schema UI) Swashbuckle - 从另一个项目的Model中向Swagger UI添加模型和示例值 - Swashbuckle - Add Model and Example values to Swagger UI from a Model from another project Swashbuckle 中的默认 Swagger UI URL 是什么? - What is the default Swagger UI URL in Swashbuckle? 将身份验证添加到/ swagger / ui / index页面 - Swagger | Web API | Swashbuckle - Add Authentication to /swagger/ui/index page - Swagger | Web API | Swashbuckle AspNetCore Swagger / Swashbuckle如何修改xml模式请求 - AspNetCore Swagger/Swashbuckle how to modify xml schema requests 使用 Swagger 6.0.0-rc1-final 在 Swagger UI 上的模型架构中分配默认请求数据 - Assign Default Request Data in Model Schema on Swagger UI using Swagger 6.0.0-rc1-final 我可以配置Swagger UI以使用Swashbuckle接受并清空数组吗? - Can I configure Swagger UI to accept and empty array using Swashbuckle? 迁移到 Swashbuckle.AspNetCore 版本 5 时,Swagger UI 中的不记名身份验证 - Bearer authentication in Swagger UI, when migrating to Swashbuckle.AspNetCore version 5 在 swagger-swashbuckle UI 中仅显示选定的控制器 - Show only selected controllers in swagger-swashbuckle UI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM