Max.Futerman
Max.Futerman

Reputation: 1142

Make parameters in swashbuckle optional(not required)

I want to make param in my controller as optional but swagger shows it as required.

my controller looks like:

[HttpGet("{name}")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicy(string name)
 {
     if (string.IsNullOrEmpty(name))
     {
         return BadRequest("Name is empty");
     }
     try
     {
         CRUDPolicyResponse crudPolicyResponse = await _managementOperations.CRUDPolicy(CRUDType.Read, name, null);
         if (!crudPolicyResponse.OperationSucceeded)
         {     
            return StatusCode((int)HttpStatusCode.BadRequest, crudPolicyResponse.Message);
         }
         if (crudPolicyResponse.FileMetadataPolicy == null)
         {
             return NotFound($"Policy name doesn't exists NAME: {name}");
         }
         return Ok(crudPolicyResponse.FileMetadataPolicy);
     }
     catch (Exception ex)
     {
        _log.Error("Error while trying to save file meta data policy", ex);
            return StatusCode((int)HttpStatusCode.InternalServerError, ex);
     }
 }

I tried to change to default value like this: string name = null but not working/ swashbuckle looks like this

So the name string is required and i can't make get with name as empty.

I tried to solve my problem with this solution make int as nullable

Upvotes: 16

Views: 14476

Answers (4)

Terai
Terai

Reputation: 331

Change your param using FromQuery like this :

public async Task<IActionResult> GetPolicy([FromQuery] string name)

Upvotes: 2

theGleep
theGleep

Reputation: 1187

I recently had this issue, myself, and I resolved it by adding a second endpoint:

Your original signature:

[HttpGet("{name}")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicy(string name)

and the new one:

[HttpGet("")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicy()

Naturally, this led to refactoring the body of the method, and the two endpoints became arrow methods:

protected async Task<CRUDPolicyResponse> GetPolicy(string name = null) {
...
}

[HttpGet("{name}")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicyByName(string name) =>  OK(await GetPolicy(name));

[HttpGet("")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicyAnyPolicy() => OK(await GetPolicy());

Upvotes: -3

ToDevAndBeyond
ToDevAndBeyond

Reputation: 1503

Add the default value to your controller parameter

[HttpGet("{name}")]
[SwaggerResponse((int)HttpStatusCode.OK)]
[SwaggerResponse((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> GetPolicy(string name = "")
 {
...
}

Upvotes: 3

Ram Y
Ram Y

Reputation: 2044

Try a DefaultValue attribute.

In your case, that would be:

[DefaultValue("")]

Upvotes: -1

Related Questions