Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case insensitivity in enum route binding #45590

Closed
1 task done
timgabrhel opened this issue Dec 14, 2022 · 3 comments
Closed
1 task done

Case insensitivity in enum route binding #45590

timgabrhel opened this issue Dec 14, 2022 · 3 comments
Labels
Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Status: No Recent Activity

Comments

@timgabrhel
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am trying to use enums as a route parameter in my .net 7 minimal api, with the flexibility to have case insensitivity in the parameter binding. I've done this in .NET 6 controller based APIs which worked with identical configuration (seen below), but the behavior doesn't match here.

I am avoiding trying to write a custom parameter binding as I wish to let Swagger auto-discover the enum parameter and hydrate the enum options properly. This API spec is shared outside of my team and the documentation built in with the enums is incredibly valuable.

Expected Behavior

POST /optiona resolves the endpoint and returns the string OptionA

Steps To Reproduce

dotnet new webapi -minimal

using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(o =>
{
    o.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    o.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
    o.SerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});

var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.MapGet("/{myenum}", ([FromRoute] MyEnum myenum) =>
{
    return Results.Ok(myenum.ToString());
});

app.Run();

public enum MyEnum
{
    OptionA,
    OptionB
}
  1. Navigate to /optiona and be shown an error

BadHttpRequestException: Failed to bind parameter "MyEnum myenum" from "optiona".

  1. Navigate to /OptionA and see "OptionA" in the response body of the browser.

Exceptions (if any)

No response

.NET Version

7.0.100

Anything else?

VS Code

.NET SDK:
 Version:   7.0.100
 Commit:    e12b7af219

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /usr/share/dotnet/sdk/7.0.100/

Host:
  Version:      7.0.0
  Architecture: x64
  Commit:       d099f075e4

.NET SDKs installed:
  7.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found
@Tratcher Tratcher added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Dec 14, 2022
@captainsafia
Copy link
Member

Triage: JSON options affect the way the enum is parsed if it is present in a JSON payload to the request. Since your enum is provided as a route parameter, these JSON options won't take affect.

This behavior is dictated by the logic in our parameter binding, which uses the Enum.TryParse overload that does not enable case-insensitivity.

You might want to consider implementing your own TryParse implementation on a wrapper for your MyEnum type that is case-insensitive. See more info at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/parameter-binding?view=aspnetcore-7.0#tryparse

@captainsafia captainsafia added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Dec 15, 2022
@ghost
Copy link

ghost commented Dec 15, 2022

Hi @timgabrhel. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@ghost
Copy link

ghost commented Dec 19, 2022

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Dec 22, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 21, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Status: No Recent Activity
Projects
None yet
Development

No branches or pull requests

3 participants