Swashbuckle.AspNetCore: Enum string converter is not respected using .NET 6 minimal API
Using Swashbuckle.AspNetCore 6.2.3 with ASP.NET 6 minimal API, I have configured JSON serializer options per the docs.
services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
However, Swagger UI still reports enums as ints unless I add also use AddControllers().AddJsonOptions():
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
Once the second block is added, the swagger UI output references the enum string values as expected. As far as I know, the latter simply does the former under the hood, but there seems to be some order of operations affecting the schema generation here?
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 22
- Comments: 18
I ran into the same problem and my colleague suggested the following as a workaround, which works for me:
You can always use NewtonsoftJson it is in nuget
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.2" />
here are my settings
.AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.Converters.Add(new StringEnumConverter()); options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc; });
Swashbuckle.AspNetCore 6.4.0 picks up changes from “Microsoft.AspNetCore.Mvc.JsonOptions” options. However, minimal APIs are using “Microsoft.AspNetCore.Http.Json.JsonOptions”. Therefore, both of them needs to be configured for Swagger schemas to match the API output.
It would be nice if Swashbuckle can be configured to use “Microsoft.AspNetCore.Http.Json.JsonOptions”.
The below workaround works on the swagger schema.
However, I had to use
AddContollers
to get it working with the swagger example.Has anyone been able to get it working with the swagger example without adding
AddControllers
?@wrkntwrkn and @stvwndr helped lead me to the workaround that I’m using:
Thank you, this works as expected!
To anyone facing a similar issue as me, make sure you have the right namespace when registerting your serializer globally.
NOT - Microsoft.AspNetCore.Mvc - JsonOptions YES - Microsoft.AspNetCore.Http.Json - JsonOptions
It was in the docs but i missed it when migrating to minimal api’s since the namings are ver similar.
Thanks this works for me. But of course decorating all enums with that is far from ideal, it would be nice if ASPNET respected the serializer converter.
I managed to get this fully working for minimal API scenarios by decorating the enum type itself with JsonConverterAttribute.
Tested on NET7 and NET8 RC1:
Looking into this some more, it looks like the problem may be in SwaggerGenServiceCollectionExtensions.cs, line 34:
The problem here is that the JsonOptions type is pulled from the
Microsoft.AspNetCore.Mvc
namespace while .NET 6 minimal APIs are documented to use JsonOptions from theMicrosoft.AspNetCore.Http.Json
namespace which this code seems blissfully unaware of. Not sure what the path forward is.