Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 6 Swagger: How to display enums as strings?

I am trying to add enums as parameters for my Swagger endpoint but they are being displayed as integers:

swagger example

On the old .NET there was an option for Swagger option.DescribeAllEnumsAsStrings(); but I don't have it on .NET 6:

program code example

I tried adding:

options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());

but it is still not showing my enums as strings.

like image 387
Tenio Hristov Avatar asked Jan 25 '26 01:01

Tenio Hristov


1 Answers

I found the same issue here, and I test the workaround mentioned in the question in my side and it worked for me.

In Program.cs,

Append .AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); behind builder.Services.AddControllers()

The code should look like:

using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers()
    .AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); });

enter image description here

like image 138
Tiny Wang Avatar answered Jan 26 '26 16:01

Tiny Wang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!