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
34.89k stars 9.85k forks source link

Json Converter option ignored while binding enum from query #49398

Closed manuelelucchi closed 1 year ago

manuelelucchi commented 1 year ago

Is there an existing issue for this?

Describe the bug

I have a controller method with a class as a query parameter, class with an enum as a field like below

class ControllerExample
{
   public IActionResult Method([FromQuery] Example parameter) { ... }
}

class Example
{
    public MyEnum Value {get;set;}
}

enum MyEnum
{
   InProgress
}

Since I want enums to be treated as strings and everything to be serialized as snake case, I configured the host json options as follows

builder.AddJsonOptions((JsonOptions options) =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = new SnakeCaseNamingPolicy();
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(new SnakeCaseNamingPolicy()));
});

with SnakeCaseNamingPolicy is a custom policy I found on the internet and tested, working correctly (no idea why it isn't supported by default like the Camel Case one)

Everything works fine in the application, except this specific interaction: while other fields in example are correctly converted using snake case, the Enum value is trying to be converted using camel case (also it ignores the casing, so inprogress works too)

Is this by design? Is there a way to make it reactive to these options?

Expected Behavior

The Model Binder should reflect the json options for the rest of the app

Steps To Reproduce

A code similar to the one posted above

Exceptions (if any)

No response

.NET Version

7.0.304

Anything else?

.NET SDK: Version: 7.0.304 Commit: 7e794e2806

Ambiente di runtime: OS Name: Windows OS Version: 10.0.22621 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.304\

Host: Version: 7.0.9 Architecture: x64 Commit: 8e9a17b221

.NET SDKs installed: 7.0.304 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

davidfowl commented 1 year ago

JSON serializer options only apply to JSON serialization e.g. the body. Not the query, or the form or headers...

manuelelucchi commented 1 year ago

Alright, I'll close the issue then, thank you. The only way to achieve this behavior is to create a custom model binder?