在 ServiceStack 中传达必需/可选 DTO 属性的最佳方式是什么? [英] What's the best way to convey required/optional DTO properties in ServiceStack?
问题描述
我的 ServiceStack w/Swagger 实现在记录必需/可选属性时遇到问题.实现使用我的服务的客户端的开发人员喜欢 Swagger 文档,但是他们不知道哪些属性是必需的还是可选的——除了每次尝试获取有效请求时都会得到 400 响应.
I'm having an issue with my ServiceStack w/ Swagger implementation regarding documenting required/optional properties. Developers implementing clients that consume my services love the Swagger documentation, however they don't know which properties are required vs. optional--aside from getting a 400 response on each attempt to get a valid request through.
举个例子:
public class UserProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserAddress Address { get; set; }
}
public class UserAddress
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public string PhoneNumber { get; set; }
}
如果这两种类型是我的 DTO 的一部分,Swagger 将清楚地显示它们,但是我无法传达是否需要 FirstName、LastName 或任何 Address 属性.有没有一种方法可以在不必滚动单独的规范文档的情况下实现这一点?
Swagger will cleanly show both of these types if they are part of my DTO, however I can't convey that FirstName, LastName, or any of the Address properties are required or not. Is there a way to accomplish this without having to roll a separate spec document?
推荐答案
您可以在 DTO 中的属性上使用 [ApiMember(IsRequired = false)]
属性为 swagger ui 添加额外信息.
You can use an [ApiMember(IsRequired = false)]
attribute on the properties in the DTO to add extra information for swagger ui.
这篇关于在 ServiceStack 中传达必需/可选 DTO 属性的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!