Skip to content

Enum as API method's parameter ignoring naming policy when binding #56907

@johnwc

Description

@johnwc

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

We have an Enum as one of the method's parameters. We also have a specific naming policy for that Enum. All the API responses that contain the Enum are getting formatted correctly as kebab naming within the JSON responses. But, when used as a parameter the binding fails to use the correct converter, it only works if the name is formatted as the default naming policy.

Expected Behavior

The binding for the parameters utilize the same naming policy that the Enum is configured for.

Steps To Reproduce

Configure specific Enum with custom naming policy.

.AddJsonOptions(options =>
{
    ...
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter<Providers>(System.Text.Json.JsonNamingPolicy.KebabCaseLower));
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(System.Text.Json.JsonNamingPolicy.CamelCase));
    options.JsonSerializerOptions.Converters.Add(new Core.Converters.DateOnlyJsonConverter());
});

Use Enum as type of a parameter.

[HttpPost("{provider}/unlink", Name = "Unlink")]
public async Task<ActionResult<BooleanResponse>> Unlink(Providers provider)
{
    ...
}
public enum Providers
{
    ProviderOne,
    ProviderTwo,
    ProviderThree,
}

Exceptions (if any)

When calling api endpoint as https://example.com/provider-one/unlink the server responds with 400.
When calling api endpoint as https://example.com/providerOne/unlink the method gets executed and the server responds with 200.

.NET Version

8.0.303

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions