dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.52k stars 10.04k forks source link

OpenApi - Does not respect JsonStringEnumConverter #59057

Closed deinok closed 1 hour ago

deinok commented 3 hours ago

Is there an existing issue for this?

Describe the bug

As the title says, the new OpenApi package does not respect "JsonStringEnumConverter". It outputs enums as "integer".

Expected Behavior

The OpenApi takes into consideration the JsonStringEnumConverter and does not output the enums as its underlying primitive

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.0

Anything else?

No response

martincostello commented 3 hours ago

Where are you using the converter? Attributes in properties of types, or global configuration of MVC/HTTP options?

Some code that reproduces exactly what you're seeing would help.

deinok commented 3 hours ago

@martincostello Globaly in the ConfigureServices

serviceCollection.AddControllers()
    .AddJsonOptions(jsonOptions =>
    {
        jsonOptions.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
    });
captainsafia commented 1 hour ago

@deinok Thanks for filing this issue!

Unfortunately, you're running into a long-standing issue with the way configuring JsonOptions evolved in ASP.NET Core. The JSON options instance that you modify when calling .AddControllers().AddJsonOptions() is local to MVC and doesn't get picked up by other components in ASP.NET Core. For that, you'll need to modify the lower-level HTTP-based JSON options via IServiceCollction.ConfigureJsonOptions.

You can find more background on this problem in this earlier reported issue over at https://github.com/dotnet/aspnetcore/issues/57891.

deinok commented 1 hour ago

Closing in favour of #57891