Version: 3.0.0
Given the following controller:
[HttpGet("")]
[SwaggerResponse(200, typeof(CollectionResponse<Bar>), "Returns the list of bars.")]
public Task<IActionResult> GetBars([FromQuery] BarListFilter filter = BarListFilter.All)
The following swagger.json is generated:
{
"name": "filter",
"in": "query",
"description": "The filter",
"required": false,
"type": "string",
"default": 0,
"enum": [
"all",
"debug",
"error"
]
}
As you can see, the default enum is shown as the enum ordinal (0) while the enums are shown as strings. I would have expected the default to also be shown as a string? Thanks.
Edit: Cleaned up formatting..
Here is the workaround for the issue. You can implement IParameterFilter interface and assign default value to 'desired string value' explicitly.
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new OpenApiInfo
{
Title = "Swashbuckle Sample API",
Version = "v1",
....
});
c.ParameterFilter<EnumDefaultValueParameterFilter>();
...........................
});
public class EnumDefaultValueParameterFilter : IParameterFilter
{
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
if ( parameter.Name.Equals("filter"))
{
PartialSchema nbp = parameter as PartialSchema;
nbp.Default = "All";
}
}
}

Many thanks for the workaround, it worked perfectly.
Most helpful comment
Many thanks for the workaround, it worked perfectly.