Describe an array of complex objects using Swagger 2.0

青春壹個敷衍的年華 提交于 2019-12-12 02:25:54

问题


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.



来源:https://stackoverflow.com/questions/38088722/describe-an-array-of-complex-objects-using-swagger-2-0

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