Question

I have just gotten Swagger up and running and so far its really impressive. I am trying to get some initial endpoints working for a client and I am getting hung up on the [ApiMember] attribute.

So consider this as my request dto (not real just an example):

[Route("/person", "POST")
public class Person
{
    public ing Age { get; set; }
    public string Name { get; set; }
}

This will display in Swagger as a parameter type of body.

But I want to convey optional properties... Say for example the Name property is optional. So my assumption would be something like this:

[ApiMember(IsRequired = true)]
public string Name { get; set; }

But what this does is creates the parameter as a path parameter. The example swagger api documentation displays this functionality - where a field in an object is optional. Is there a way to do this with ServiceStack and Swagger?

EDIT: I'm already aware that with value types I can set them to nullable and this will set them to optional. I'm also aware that you do not need to set reference types to nullable - but I need to somehow reflect that in the documentation.

Was it helpful?

Solution

ServiceStack Swagger integration currently always marks reference type properties in the request as required (though that could possibly be changed). You may be able to use a ModelFilter callback (specified as a property of SwaggerFeature when you register that as a plugin) to modify the Required property of each ModelProperty object sent to your callback. And you can also modify the Description of each ModelProperty with this technique, if you need additional descriptive text to mark required/optional fields.

Note that if you want to use the ApiMember attribute to annotate request body parameters, you must specify ParameterType = "body" and Name = "(name of property)" in the attribute. But there's not a lot you can do with this attribute for request body parameters currently; it's most useful for "query" or "path" ParameterTypes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top