Optional routeTemplate parameters is tagged as required in spec
See original GitHub issueconfig.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:
- Created 7 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I did some more testing on how things are handled by asp.net:
/{id} is required
/{id} is optional (but documented as required by Swashbuckle)
@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