Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 433
Jason Castellano Avatar asked Dec 19 '13 15:12

Jason Castellano


1 Answers

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

like image 59
Will Smith Avatar answered Nov 05 '22 05:11

Will Smith