-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Introduce a more ergonomic way of enabling case-insensitive enum parsing in Minimal API endpoints #48346
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
Comments
@aradalvand Thanks for filing this issue! I see there's been some conversation in the related issue around customizing parameter binding in minimal APIs. I think this particular aspects needs some design and, to be frank, some more details about the types of granular modifications that need to be made. I'll park this under |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
You can use this GitHub gist. With example usage: app.MapGet("/", ([FromQuery] EnumBinding<MyEnum> param) =>
{
MyEnum enumValue = param;
}); |
To add to @adamijak's workaround for those that want a proper Swagger documentation, you can add an schema filter like this one: public class EnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
var type = context.Type;
if (type == null)
{
return;
}
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(EnumBinding<>))
{
var enumType = type.GetGenericArguments().FirstOrDefault(x => x.IsEnum);
if (enumType == null)
{
return;
}
var names = Enum.GetNames(enumType);
if (names == null)
{
return;
}
schema.Type = "string";
schema.Enum = names.OfType<string>().Select(x => new OpenApiString(x)).ToList<IOpenApiAny>();
}
}
} |
@jrebagliatti @captainsafia I updated the gist. Now it accepts boolean and DateTime types as-well. |
Basically the same as #45590.
Currently, the only way to make this work would be to create another type that acts as a wrapper around the enum, just to implement a
TryParse
method that does case-insensitive parsing for the underlying enum; as @captainsafia described in the aforementioned issue:But it goes without saying that this is incredibly ugly.
So, either:
Enum.TryParse
overload that does a case-insensitive comparison. This way the behavior will also be consistent with that of MVC controllers. Or provide a global opt-in option that lets the developer decide.TryParse
orBindAsync
method on the type of the parameter. This is a direly needed feature in general; as you don't always have access to the type of a parameter in such a way that you can declare a staticTryParse
orBindAsync
method on them (as is effectively the case for enums, or for types that come from third-party libraries), so it should be possible to customize binding for individual parameters in a granular fashion, as opposed to having to modify the types themselves.The second option is of course preferable.
The text was updated successfully, but these errors were encountered: