Swashbuckle.aspnetcore: [FromQuery] Default enum parameter is shown as an ordinal and not a string

Created on 29 Aug 2018  路  2Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

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..

Most helpful comment

Many thanks for the workaround, it worked perfectly.

All 2 comments

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";
        }
        }
}

image

Many thanks for the workaround, it worked perfectly.

Was this page helpful?
0 / 5 - 0 ratings