Swagger UI adds extra slash when 2 optional parameters are null and Web Api does not hit the Route

Viewed 77

I have configured Swagger to use optional parameters. Here is action definition

[SwaggerOperationFilter(typeof(OptionalRouteParameterOperationFilter))]
[HttpGet("[action]/{someId}/{anotherId}/{startDate?}/{endDate?}")]
public ActionResult<IEnumerable<Model>> GetSomething(int someId, int anotherId, [FromRoute] DateTime? startDate = null, [FromRoute] DateTime? endDate = null)

here how swagger screen looks like

Heads up: when using Postman, this all works fine.

But when using swagger ui I get this URL

https://localhost:5001/api/SomeController/GetSomething/55/67//

And that last slash / is the issue

I get

Error: response status is 404

Looking for ideas how to fix swagger. Note: I know about "Query string idea". Thanks

Update 1: Swagger config in startup

services.AddSwaggerGen(opt =>
{
    opt.EnableAnnotations();
    opt.DescribeAllParametersInCamelCase();
});

Update 2: Something completely left my mind. Important. How did I derive OptionalRouteParameterOperationFilter? This is a custom class that looks like this (irrelevant code removed)

public class OptionalRouteParameterOperationFilter : IOperationFilter
{
  . . . . 
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
     . . . . . 
        
        var parameter = operation.Parameters.FirstOrDefault(p => p.In == ParameterLocation.Path && p.Name == name);
        if (parameter != null)
        {
            parameter.AllowEmptyValue = true;
            parameter.Description = "<b>***</b>Check <i>\"Send empty value\"</i> for correct empty parameter operation";
            parameter.Required = false;
            parameter.Schema.Nullable = true;
        }
 . . . . . 
0 Answers
Related