Describe an array of complex objects using Swagger

2019-09-09 16:55发布

问题:

I have a Web Api 2 project that has an HttpGet method that takes a list of complex objects as the parameter. Like this:

[HttpGet]
public string GetCoolStuff(List<ContainerContract> containers)

I am using swashbuckle to setup my swagger docs. But it sets this parameter up like this:

{
  "name" : "containerContracts",
  "in" : "query",
  "required" : true,
  "type" : "array",
  "items" : {},
  "collectionFormat" : "multi"
}

At the very least the items object seems like it needs something in it.

Later down in the definitions section I did find this:

"ContainerContract" : {
    "type" : "object",
    "properties" : {
        "Type" : {
            "type" : "string"
        },
        "Temperature" : {
            "type" : "string"
        },
        "CreatedWhen" : {
            "format" : "date-time",
            "type" : "string"
        }
    }

But it does not seem to be used...

Is there a way I can set this up to have swagger understand that this is a list of objects and give me a way to enter values for the individual properties? (Like I do for complex objects that are not lists.)

Here is what I mean in a picture:

Or is Swagger Ui just not that smart yet? (If I have to write Json to fill in my lists then I can.)


Just incase it matters, here is an example of ContainerContract:

public class ContainerContract
{
    public string Type { get; set; }
    public char Temperature { get; set; }
    public DateTime CreatedWhen { get; set; }
}

回答1:

unfortunately in the 2.0 specification (which Swashbuckle uses), you cannot have complex objects in query parameters, even in arrays. So what you're looking to do is not supported until the next version. You can have an array, but the items must be a primitive value type.