question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional routeTemplate parameters is tagged as required in spec

See original GitHub issue
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
public class ValuesController : ApiController
{
    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }
}

The path parameter gets documented as required in swagger.json:

{
  "/api/Values": {
    "get": {
      "operationId": "Values_Get"
    }
  },
  "/api/Values/{id}": {
    "get": {
      "operationId": "Values_Get",
      "parameters": [
        {
          "name": "id",
          "in": "path",
          "required": true,
          "type": "integer",
          "format": "int32"
        }
      ]
    }
  }
}

Same result using a nullable int

public class ValuesController : ApiController
{
    public string Get(int? id)
    {
        return "value";
    }
}

Or optional string

public class ValuesController : ApiController
{
    public string Get(string id = null)
    {
        return "value";
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
moandercommented, Aug 18, 2016

I did some more testing on how things are handled by asp.net:

/{id} is required

    public string Get(int id)

    public string Get(int? id)

    public string Get(string id)

/{id} is optional (but documented as required by Swashbuckle)

    public string Get(int? id = null)

    public string Get(string id = null)
2reactions
domaindrivendevcommented, Oct 4, 2016

@moander - that’s exactly the approach I would recommend. It keeps your contract intact and minimizes code changes because the new overloads can delegate to your existing method - which should be made private

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional routeTemplate parameters is tagged as required ...
Swagger doesn't support optional "path" parameters because the path (less query string) uniquely identifies an operation.
Read more >
Optional Route Parameters with Swagger and ASP.NET Core
I'm here to show you how to make optional route parameters with Swagger and ASP.NET Core. Before we begin let's evaluate the scenario....
Read more >
ASP.NET5 MVC 6 routing with optional parameter / default ...
Optional URI Parameters and Default Values. You can make a URI parameter optional by adding a question mark to the route parameter.
Read more >
Optional Parameters in ASP.NET Core Web API Routing
In a route template, an optional parameter is a URI or endpoint parameter that we can remove since default values have been supplied...
Read more >
Routing in ASP.NET Core
Discover how ASP.NET Core routing is responsible for matching HTTP requests and dispatching to executable endpoints.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found