Home > other >  Where does this "Required" attribute for the Swagger UI coming from for ASP.NET?
Where does this "Required" attribute for the Swagger UI coming from for ASP.NET?

Time:12-25

I have the following in my ASP.NET Controller:

[Produces("application/json")]
[Consumes("application/json")]
[Route("api/[controller]")]
[ApiController]
public class ConnectionManagersController : ControllerBase 
{
    [HttpGet("{connectionManagerID:int}")]
    [ProducesResponseType(typeof(ConnectionManagerModel), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int connectionManagerdID){}
}

However when I run the app and the Swagger UI comes up I get the following screen:

enter image description here

There are TWO connectionManagerID fields on the Swagger UI - the first is an int (which is should be) and the second is a string and is required which I dont know where that is coming from.

I dont know where the *required field is coming from.

CodePudding user response:

The parameter connectionManagerdID is an int - it is not nullable so in effect it is required.

CodePudding user response:

Try this:

public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int connectionManagerdID = null){}  

It should set it to optional.

CodePudding user response:

it is from here, you will probably get 404, if you don't supply id to url.

[HttpGet("{connectionManagerID:int}")]

and if you don't want to be required try this

[HttpGet("{connectionManagerID?:int}")]

in this case maybe it makes sense to change the action too

public async Task<ActionResult<ConnectionManagerModel>> GetConnectionManagerAsync(int? connectionManagerdID){}  

or you can keep what you have. It will be 0 by default.

  • Related