简体   繁体   中英

Swashbuckle Swagger: How to show type of array of models parameter?

I am using latest 2.4 version of https://github.com/domaindrivendev/Swashbuckle.AspNetCore to generate documentation for AspNetCore Controller method having 2 parameters: guid and list of models.

[HttpPost("MyMethod/{ReferenceId}")]
public async Task<IActionResult> MyMethod(Guid referenceId, List<ApiProfile> passengers)

The first parameter generates correctly, but the second doesn't understand type and shows just array(no type)

Passengers
array
(query)

And in json:

"parameters": [
    {
        "name": "passengers",
        "in": "query",
        "description": "",
        "required": false,
        "type": "array",
        "items": {},
        "collectionFormat": "multi"
    },

Note that the type ApiProfile is shown at the bottom under Models section:

ApiProfile {
    description:    API Object
    Id  string($uuid)
    Email   string
    salutation  string
    firstName   string
    lastName    string
    dateOfBirth string($date-time)
}

And in json:

"definitions": {
    "ApiProfile": {
        "description": "API Object",
        "type": "object",    
        "properties": {"Id": {"format": "uuid","type": "string"},
        "Email": {"type": "string"},
        "salutation": {"type": "string"},
        "firstName": {"type": "string"}
...

I need an advice how to describe type of List of request parameters to be shown in Swagger UI.

I've tried to assign [SwaggerRequestExample(typeof(PeopleRequest), typeof(ListPeopleRequestExample))] but not sure how to do it with 2 parameters. Also I tried to temporary exclude first parameter, but have the same behaviour.

Update: I've created a minimal version, but have the same behaviour.

  [Route("[controller]")]
    public class SwashbuckleTest : Controller
    {
         [HttpPost]
        [Route("{id}")]
        public SwashbuckleTestProfile Post(Guid id, List<SwashbuckleTestProfile> companies)
        {
            return companies.FirstOrDefault();
        }
    }
    public class SwashbuckleTestProfile
    {

        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Full example can be loaded from https://github.com/MNF/Samples/tree/master/SwashbuckleExample

You should not have to do anything special or describe anything.
Swashbuckle should take care of that... Smells like a bug

I tested something similar with my project Swagger-Net and it renders ok:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=MultiParam#/MultiParamPost/MultiParamPost_Post

    [Route("{id}")]
    public Company Post(Guid id, List<Company> companies)
    {
        return companies.FirstOrDefault();
    }

Here is the relevant JSON that code outputs:

      {
        "name": "companies",
        "in": "body",
        "required": true,
        "schema": {
          "items": {
            "$ref": "#/definitions/Company"
          },
          "xml": {
            "name": "Company",
            "wrapped": true
          },
          "type": "array"
        }
      }

I guess if you really want to get Swashbuckle you can use an IDocumentFilter and change the schema to look more like mine.

Update: After the playing with provided minimal version, it seems that adding [FromBody] makes a big change to the schema in Swashbuckle.AspNetCore

    [Route("{id}")]
    public Company Post(Guid id, [FromBody]List<Company> companies)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM