Marius
Marius

Reputation: 1574

.Net 5 - Swagger UI - Required Enum in form requires a change to succeed

Have some weirdness with Swagger UI for a .Net 5 WEB API.

I've got required Enums in an object. Which is displaying and setting correctly. But you cannot execute the endpoint without changing the value in the dropdown.

Is there a way I can make it, that the default set value is a valid value when executing?

Example:

Enum validation failure example

C#:

[Required]
public SizeEnum Size { get; set; }

Upvotes: 0

Views: 6959

Answers (1)

Rena
Rena

Reputation: 36575

Implement the following for each enum:

public class Test
{
    [Required]
    [DefaultValue(SizeEnum.Small)] //Default value must be set using System.ComponentModel, and is required to make this work correctly.
    public SizeEnum Size { get; set; }
}
public enum SizeEnum
{
    Small,
    Large
}

Update Startup.cs with the following:

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebapiProject5", Version = "v1" });
    c.UseInlineDefinitionsForEnums();  //add this...
});

Upvotes: 4

Related Questions