What's the best way to convey required/optional DTO properties in ServiceStack?

心不动则不痛 提交于 2019-12-10 04:29:37

问题


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.

Take the following example:

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; }
}

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?


回答1:


You can use an [ApiMember(IsRequired = false)] attribute on the properties in the DTO to add extra information for swagger ui.

There is list of the attributes that swagger ui will recognise on the servicestack wiki



来源:https://stackoverflow.com/questions/20685296/whats-the-best-way-to-convey-required-optional-dto-properties-in-servicestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!