[Feature] DefaultValue for operation parameters defined by class
See original GitHub issueModify SwaggerGenerator so that it takes DefaultValueAttribute
into account.
Given following operation:
/*
* Matches following urls:
* /api/myresource/
* /api/myresource?skip=10
* /api/myresource?skip=10?take=20
*/
[HttpGet]
public IActionResult Index(MyQueryParams query)
{
}
public class MyQueryParams
{
[DefaultValue(0)]
public int Skip { get; set; } = 0;
[DefaultValue(10)]
public int Top { get; set; } = 10;
}
Swashbucle should generate the parameters as Optional and with Default value:
"get": {
"parameters": [
{
"name": "skip",
"in": "query",
"required": false,
"default": 0,
"type": "integer",
"format": "int32"
},
{
"name": "top",
"in": "query",
"required": false,
"default": 10,
"type": "integer",
"format": "int32"
}
]
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:10
- Comments:29 (7 by maintainers)
Top Results From Across the Web
c# - Swagger default value for parameter
I've always set the default on the param itself like this: public class TestPostController : ApiController { public decimal Get(decimal x ...
Read more >Describing Parameters
In OpenAPI 3.0, parameters are defined in the parameters section of an operation or path. To describe a parameter, you specify its name...
Read more >Named and Optional Arguments - C# Programming Guide
Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value...
Read more >Parameters in UML class operations
A parameter has a type and it can have a default value. A parameter specifies the type of an argument and the value...
Read more >Why I “hate” optional parameters in C# | by The Code ...
Optional does not mean Default. One of the biggest misuses of optional parameters is that of providing a default argument to a function....
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 Free
Top 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
@Liero you can add an operation filter as a quick fix, I wrote one that supports both property
DefaultValue
attributes and default values in method parameters:Default Value Attribute is still ignored when being used as FromQuery in versions 4.0.1 and 5.0.0. @whyleee’s fix is very appreciated.