2

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;
        }
 . . . . . 
T.S.
  • 18,195
  • 11
  • 58
  • 78
  • 1
    can you post your swagger settings from startup.cs ? – Pribina Jul 20 '22 at 05:03
  • check [this](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/406#issuecomment-752259922) from github issues of swashbuckle. – CodingMytra Jul 20 '22 at 05:10
  • 1
    Are the parameters optional but supposed to appear together if they do appear? How would you provide `endDate` without `startDate` unless there is a delimeter in the route? Can you register two routes with and without the date range? Does this give any ideas [Multiple optional parameters routing](https://stackoverflow.com/a/27710023/371)? – Dave Anderson Jul 20 '22 at 05:48
  • 1
    OpenAPI Specification [does not allow](https://stackoverflow.com/a/35030135/113116) optional path parameters. Path parameters are always required. You'll have to define multiple endpoints - one with both `startDate` and `endDate` present, one with just `startDate`, and another without `startDate` and `endDate`. Alternatively, change `startDate` and `endDate` to query parameters - these can be optional. – Helen Jul 20 '22 at 09:34
  • @DaveAnderson Good question. In this situation, if dates are actually involved, we always have the `startDate` and the `endDate` is optional. Can I have 2 routes? potentially - sure. This is not a preference though.... Good article-I will look more into it. Thank you – T.S. Jul 20 '22 at 12:28
  • @Helen Thanks for the tip. Not enough hands to read everything first. I definitely would rather comply with the standards. However, from reading what in the linked spec document I don't know how to derive to the conclusion, as they posted, this - "Given that path parameter must be required according to the OpenAPI/Swagger spec," Someone would need to literally walk me through this spec. Swagger seem allow optional URL items, only leaves `/`. I have to move with something - extra routes or Query String. This question becomes academic at this point – T.S. Jul 20 '22 at 14:50
  • @Pribina please see "Update 1" in question – T.S. Jul 20 '22 at 14:53
  • 1
    @T.S. w/r/t "path parameters must be required", here's an excerpt from the spec section ["Parameter Object"](https://spec.openapis.org/oas/v3.1.0.html#parameterRequired): _"`required`: If the parameter location is "path", this property is REQUIRED and its value MUST be `true`."_ Your current route is not OpenAPI-compliant. Swagger UI doesn't validate API definitions, but if you [export the definition](https://stackoverflow.com/q/48525546/113116) and paste it into https://editor.swagger.io, you'll see the validation errors. The correct approach is either multiple routes or query parameters. – Helen Jul 20 '22 at 15:29
  • 1
    @Helen A-ha!!! Now, this document makes much sense to me. You should post this as answer under heading of "if you want to be openApi compliant". Now, this all comes together. I also Updated my question because I forgot most important part - how I get the check box on the screen. Phew. Thanks again – T.S. Jul 20 '22 at 15:42
  • @Helen Interestingly, I removed swagger attribute and added more route attributes to the action. Swagger shows 3 routes but each has 4 fields still – T.S. Jul 20 '22 at 15:47

0 Answers0