RequestDelegateGenerator
should use case-insensitive parsing for enum route parameters
#52497
Open
1 task done
Labels
area-minimal
Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
feature-rdg
Is there an existing issue for this?
Describe the bug
RequestDelegateGenerator
should use case-insensitive parsing for enum route parameters.This has been brought up before, most recently by @aradalvand in #48346, but those previous issues were treated as opportunities for future extensibility, whereas I would like to make the case that this is a
bug with user-facing impactwell-documented pinch point that should be easy to patch in a post-RDG world.Argument 1: prior art from other primitives with lenient default parsing rules
Enums (unfortunately) didn't receive the same
IParsable<T>
treatment fornet8.0
as other first-class primitives, which means that RDG has to make a choice about which overload ofEnum.TryParse(...)
to call, as opposed to delegating toIParsable<T>.TryParse(...)
.But if we look around at how
IParsable<T>
was implemented/delegated for other primitives, we can find examples erring on the side of more lenient parsing rules by default.For example, here are the first few lines of the implementation for
IParsable<bool>.TryParse(...)
:While there's nothing to stop anyone from chaotically defining enum members that differ only in casing, it's a broadly respected convention (backed on Roslyn analyzers) for .NET enums to have members whose names differ by more than case.
Further, .NET enums broadly follow the same conventions as other .NET types, meaning the gross majority of (normal) enums in the wild are defined in PascalCase. This means that any use of an enum in a RDG-generated handler will suffer from the unexpected case-sensitivity demonstrated below in Argument 3.
(I anticipate and respect the quibble that this shouldn't be unexpected, since its well-documented, but if minimal APIs are about making .NET easy to work with out-of-the-box, then it's still a problem worth solving.)
Argument 2: RDG has enough context to decide when case-sensitivity makes sense
The web is case-insensitive.The web should be case-insensitive.The web was, is, and always shall be a place of chaos, but we still deserve sensible happy-path defaults.
It makes sense to use case-sensitive enum parsing by default for things like query strings, request bodies, etc. However, it makes much less sense to do the same in places like path segments, headers, etc.
RDG should have enough context available to generate case-insensitive parsing for path parameters without having to do so for non-path parameters.
Argument 3: the rest of the routing stack treats path segments as case-insensitive
The real-world question I received today was: "why is only part of this URL case-sensitive?"
The answer was immediately obvious, because I already knew better than to use raw enums in route templates, but I still managed to walk right into it for the umpteenth time. (...and again, I have a sneaking suspicion that I can't be the only one repeatedly stubbing their toes on this.)
Culprit
aspnetcore/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/EndpointParameter.cs
Lines 323 to 335 in b28c8a3
Related
Expected Behavior
Steps To Reproduce
Exceptions (if any)
No response
.NET Version
8.0.100
Anything else?
No response
The text was updated successfully, but these errors were encountered: