Using ServiceStack Swagger Plugin, how to implement a string field with a list of preset values

I am embedding the Swagger API documentation using the new ServiceStack Swagger plugin and trying to determine how to use the container data type. I need to display a string field that has a list of predefined values ​​and other parameters that are lists of sub-objects.

If I am missing something, I believe that swagger can only accept a text field into which you enter JSON for the list of sub-objects. I believe this code should do the trick.

[ApiMember(Name = "Connections", Description = "insert JSON sample here", ParameterType = "body", DataType = "container", IsRequired = false, Verb = "Post")] 

That I do not know (and I hope that someone there can help me), if it is possible to have a string field that is from the list of given values. In Swagger, this piece of code illustrates how to do this.

 "Pet":{ "id":"Pet", "properties":{ ... "status":{ "type":"String", "description":"pet status in the store", "allowableValues":{ "valueType":"LIST", "values":[ "available", "pending", "sold" ] } }, "happiness": { "type": "Int", "description": "how happy the Pet appears to be, where 10 is 'extremely happy'", "allowableValues": { "valueType": "RANGE", "min": 1, "max": 10 } }, ... 

Does anyone know how this is achieved using ServiceStack.Api.Swagger?

+4
source share
1 answer

I am struggling with the same problem, but realized that this feature is not currently supported. You cannot receive POST or PUT data using models. This feature is on the move and in development, so I think it is on the task list.

If you look at the source code, you will see that the Models property is missing in the ResourceResponse :

 [DataContract] public class ResourcesResponse { [DataMember(Name = "swaggerVersion")] public string SwaggerVersion { get; set; } [DataMember(Name = "apiVersion")] public string ApiVersion { get; set; } [DataMember(Name = "basePath")] public string BasePath { get; set; } [DataMember(Name = "apis")] public List<RestService> Apis { get; set; } } 

If you compare this with the Petstore example in Wordnik, you will find that the models are included as the root node:

 { "apiVersion":"0.2", "swaggerVersion":"1.1", "basePath":"http://petstore.swagger.wordnik.com/api", "resourcePath":"/pet", "apis":[ { "path":"/pet.{format}", "description":"Operations about pets", "operations":[ { "httpMethod":"POST", "summary":"Add a new pet to the store", "responseClass":"void", "nickname":"addPet", "parameters":[ { "description":"Pet object that needs to be added to the store", "paramType":"body", "required":true, "allowMultiple":false, "dataType":"Pet" } ], "errorResponses":[ { "code":405, "reason":"Invalid input" } ] } ] } ], "models":{ "Category":{ "id":"Category", "properties":{ "id":{ "type":"long" }, "name":{ "type":"string" } } }, "Pet":{ "id":"Pet", "properties":{ "tags":{ "items":{ "$ref":"Tag" }, "type":"Array" }, "id":{ "type":"long" }, "category":{ "type":"Category" }, "status":{ "allowableValues":{ "valueType":"LIST", "values":[ "available", "pending", "sold" ], "valueType":"LIST" }, "description":"pet status in the store", "type":"string" }, "name":{ "type":"string" }, "photoUrls":{ "items":{ "type":"string" }, "type":"Array" } } }, "Tag":{ "id":"Tag", "properties":{ "id":{ "type":"long" }, "name":{ "type":"string" } } } } } 

I think the only way around this is by itself. Have a request object that accepts an entire object, such as Pet. Set ParameterType to body and DataType to Pet . In the Swagger interface, you will see a text field into which you must insert the actual JSON object. You will request the following:

 [Api("The Thing Service")] [Route("/thing", "POST", Summary = @"POST a new thing", Notes = "Send a thing here")] public class ThingRequest { [DataMember] [ApiMember(Name = "Thing", Description = "The thing", ParameterType = "body", DataType = "Thing", IsRequired = false)] public ThingDto Thing { get; set; } } 

And your service:

 /// <summary> /// Summary description for ThingService /// </summary> public class ThingService : Service { public IThingRepository ThingRepository { get; set; } public object Post(ThingRequest request) { var thing = Thing.Map(request); ThingRepository.Save(thing); return new ThingResponse(); } } 

The following will be shown:

Swagger output

Enter such an object and the request will be parsed correctly:

enter image description here

+3
source

All Articles