Home > Back-end >  Swagger and .net Core upgrade issue
Swagger and .net Core upgrade issue

Time:03-18

Working on a .net core app and using swagger

I recently upgraded .net core 2.1 to 3.1 and thus had to upgrade my swagger package as well from 4.0.1 to 6.3.0

With the above upgrades I see a very strange behavior to one of our api endpoints. Details as below:

Our Endpoint is as below:

[HttpGet("GetCount", Name = "GetCount")]
public async Task<IActionResult> GetUsersDataCount(string region, List<string> usersList)
{
    //code logic here
}

How the above code shows in swagger is as below:

.net Core 2.1

With above version I can add the region as parameter and usersList as array(with add item) as shown in image1

.net Core 3.1

With above version I can add the region as parameter but usersList I have to add it as RequestBody as shown in image2

With the above changes this is breaking at the team consuming this API.

Any inputs on why its behaving this way or what changes do I need to make for this to work enter image description here enter image description here

CodePudding user response:

I don't know that it would have changed from .NET Core 2.1 to .NET Core 3.1 but I'm under the impression List<T> is treated as a complex type by Web API and is therefore expected to come from the body.

You could try adding the FromUri attribute in the parameters list to explicitly mark it as part of the query parameters:

[HttpGet("GetCount", Name = "GetCount")]
public async Task<IActionResult> GetUsersDataCount(
    string region, 
    [FromUri] List<string> usersList)
{
}

More info here: Why do we have to specify FromBody and FromUri?


I've never had a problem using [FromUri] to get query parameters, but OP says [FromQuery] works.

  • Related