6

I have a WebAPI method with routing defined in an attribute, having one mandatory parameter and one optional:

    [HttpGet]
    [Route("api/ChargeCard/{cif}/{feeScheme=null}")]
    [ResponseType(typeof(ChargeCardRoot))]
    public IHttpActionResult Get(string cif, string feeScheme, ChargeCardRequestMode mode = ChargeCardRequestMode.Basic)
    {

I also use Swashbuckle / Swagger to generate documentation. The problem is that Swagger always marks my optional parameter as required.

Changing optional parameter notation to:

    [Route("api/ChargeCard/{cif}/{feeScheme?}")]

makes both parameters acting like they are required, it doesn't make Swagger to show it as optional either.

Is there a way to generate correct documentation for optional parameters with Swagger?

2 Answers 2

6

If you overload your methods, Swashbuckle will generate two different Swagger endpoints. One method has the parameter, the other does not and calls the first one with the default value for the "missing" parameter. This also has the advantage of making it easier if you using something like HyprLinkr to generate HATEOAS links, as you can't have optional parameters in an expression.

[HttpGet]
[Route("api/ChargeCard/{cif}/{feeScheme}")]
[ResponseType(typeof(ChargeCardRoot))]
public IHttpActionResult Get(string cif, string feeScheme, ChargeCardRequestMode mode = ChargeCardRequestMode.Basic)
{
    // working code
}

[HttpGet]
[Route("api/ChargeCard/{cif}")]
[ResponseType(typeof(ChargeCardRoot))]
public IHttpActionResult Get(string cif, string feeScheme)
{
    return Get(cif, feeScheme, ChargeRequestMode.Basic);
}

Hope that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, almost there :) I just had to remove feeScheme argument from the overloaded method and inside it call the other with null value, and that makes Swashbucle to generate two endpoints, and both can be now tested on Swagger page. But still, this is more like a workaround then the solution. Does it mean Swagger cannot show routing parameters as optional at all?
I don't believe you can specify optional parameters the way you are trying to do them. The problem is really the route, as it doesn't really support optional parameters that way. The parameters is the route are really separate from the parameters of the method.
This must explain why Swagger does not except optional parameters. Overload is a much cleaner way to go... imho
-1

As far as Swagger is concerned, you could specify optional parameters by omitting them from the route and specifying "string?" for the parameter declarations in the method signature as follows:

[HttpGet]
[Route("api/ChargeCard/{cif}")]
[ResponseType(typeof(ChargeCardRoot))]
public IHttpActionResult Get(string cif, string? feeScheme)
{
    ...
}

Note however, that these would no longer get passed as part of the route. The optional parameters would get passed as query parameters appended to the route, as follows:

https://somedomain.com/api/ChargeCard/{cif}?feeScheme=ABCD

If you take this approach, you would also need to add this line to the top of your file to prevent Visual Studio from removing the "?" and replacing it with [CanBeNull] attribute:

#nullable enable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.