.NET 6 Swagger show enums as strings

Thiago da Silva Adriano - Nov 19 '21 - - Dev Community

Nowadays we can use Swagger like a documentation about our API's. To help others to understand our rules when we use Enums, I'll demonstrate how we can present at Swagger our Enums as string not with magic numbers.

To this article I'll use this project swagger-enums-asstring at my GitHub.

To do this is very easy, we just need to put this code bellow at Program.cs file.

builder.Services.AddControllers()
    .AddJsonOptions(options =>
             options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
Enter fullscreen mode Exit fullscreen mode

Running our project We can see the result at image bellow:

Image description

. . . . . . . . . . . . . . .
Terabox Video Player