Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger - Web API - Optional query parameters

[HttpGet]
[Route("students")]
[SwaggerOperation(Tags = new[] { "Student" })]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(ResponseModel<IList<Student>>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(StudentResponseExample))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
public IHttpActionResult SearchStudent()
    {
        IDictionary<string, string> searchParams = null;
        searchParams = ControllerContext.GetQueryStrings();
        .
        .
        .

    }

The above API has three optional parameters which will be pass as query string

  1. SyncDate - Long
  2. OffSet - int
  3. Limit - int

There is no option for user to enter these optional query parameters in swagger UI. Please guide me to implement the optional query parameters.

I am using swashbuckle and I prefer to use annotations rather than having a lengthy comment section over each API method for swagger functionalities.

I referred the following Adding Query String Params to my Swagger Specs and created the SwaggerParameterAttribute class in Filters folder of Web API and when trying to add the OperationFilter in GlobalConfiguration.Configuration .EnableSwagger as given, it throws type or the namespace name SwaggerParametersAttributeHandler could not be found. I even added the Filters folder namespace but still the error exists.

Please guide on how to implement the optional query parameters in swagger

like image 412
Gopi Avatar asked Oct 16 '17 07:10

Gopi


People also ask

Can query parameters be optional?

As query parameters are not a fixed part of a path, they can be optional and can have default values.

How do I set optional parameters in Web API?

So, how do we make a parameter optional? We can make a route parameter optional by adding “?” to it. The only gimmick is while declaring a route parameter as optional, we must specify a consequent default value for it: [HttpGet("GetById/{id?}")]

What parameter is optional in an HTTP request?

Answer. Answer: Optional Parameters in Web API Attribute Routing and Default Values: You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.


2 Answers

This worked for me:

[System.Web.Http.HttpGet] 
[Route("api/DoStuff/{reqParam}")]  
[Route("api/DoStuff/{reqParam}/{optParam1:alpha?}/{optParam2:datetime?}")]
public string Get(string reqParam, string optParam1= "", string optParam2= "")

It did create two sections in my Swagger UI but that works for me.

like image 107
Manuel Plaza Avatar answered Oct 19 '22 04:10

Manuel Plaza


The way Swagger works it pulls out parameters based on your signature of Action i.e parameters to your Action, but here you are getting these value from ControllerContext which obviously Swagger will never be aware of.

So You need to change the signature of the Action and pass your parameters there.

They will be treated as optional if you make them of nullable type -

[HttpGet]
[Route("students")]
[SwaggerOperation(Tags = new[] { "Student" })]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(ResponseModel<IList<Student>>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(StudentResponseExample))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
public IHttpActionResult SearchStudent(long? SyncDate = null,int? OffSet = null,int? Limit = null)
{
    // Use the variables here
    .
    .
    .

}
like image 24
Ipsit Gaur Avatar answered Oct 19 '22 06:10

Ipsit Gaur