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 5 years ago
- Reactions: 35
- Comments: 28 (1 by maintainers)
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; });The following method was used to collect the Serializer settings in one place.
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:
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”.
A comment to prevent this one from closing due to inactivity, as I believe it’s still applicable and does have all the information for the reproduction.
Thank you, this works as expected!
@wrkntwrkn and @stvwndr helped lead me to the workaround that I’m using:
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.
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.Mvcnamespace while .NET 6 minimal APIs are documented to use JsonOptions from theMicrosoft.AspNetCore.Http.Jsonnamespace which this code seems blissfully unaware of. Not sure what the path forward is.The below workaround works on the swagger schema.
However, I had to use
AddContollersto get it working with the swagger example.Has anyone been able to get it working with the swagger example without adding
AddControllers?Stumbled upon this issue while debugging this issue. I can confirm the suspicion of @soligto . Mvc.JsonOptions is never null. Http.Json.JsonOptions are never considered at all.
Still an issue in version 8.1.0 on net8.0. Here’s a minimal project that reproduces the issue: https://github.com/fmelink/swashbucklerenumfail
Isn’t it because
AspNetCore.Mvc.JsonOptions.JsonSerializerOptionsis never null?https://github.com/dotnet/dotnet/blob/main/src/aspnetcore/src/Mvc/Mvc.Core/src/JsonOptions.cs#L35
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenServiceCollectionExtensions.cs#L81
This code will never get SerializerOptions from
AspNetCore.Http.Json.JsonOptions.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 can confirm that the issue still persists for me too.
Maybe the best option would be to have something like this:
builder.Services.AddSwaggerGen(options => options.EnableMinimalApi());Just noting that another approach here is the below lines:
I think the comment at https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2293#issuecomment-991870685 is correct that this is the bug because of this code: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/8f363f7359cb1cb8fa5de5195ec6d97aefaa16b3/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenServiceCollectionExtensions.cs#L33-L43
The code in the
#elseblock just uses the defaultJsonSerializerOptionsif the code isn’tNETSTANDARD2_0which includes net8.0.Thanks, this also works in NET6!
Is this workaround working on version net6.0 (6.0.100)? Dosen’t seem to work for me. Still get a serializer exception. Using System.Text.Json 6.0.2
An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Http.BadHttpRequestException: Failed to read parameter "MessageRequest messageRequest" from the request body as JSON. ---> System.Text.Json.JsonException: The JSON value could not be converted to Domain.Shared.Enums.MessageTypeEnum. Path: $.messageType | LineNumber: 2 | BytePositionInLine: 28.For now im passing in
HttpRequest httpRequestand deserializing it myself, which is not ideal/clean.