-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Fix: Detect parsability when FromRoute attribute is used #45597
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
Fix: Detect parsability when FromRoute attribute is used #45597
Conversation
What happens if you do this? webApp.MapGet("/customers/{cust}", ([FromRoute(Name = "cust")]Customer customer, string cust) => {}); I'm guessing it explodes at runtime. What happens in the parsability analyzer? (we could write an analyzer that detects this situation and warns about it, but it probably wouldn't get much usage) |
Good find btw. I need to consider using |
Oh interesting. It doesn't throw - it actually works. The route parameter value gets put into the string and TryParse gets called. I'm not entirely sure that this should be considered an error. For example the following is also considered valid: app.MapGet("/customers/{cust}", ([FromRoute(Name = "cust")] Customer aaa, [FromRoute(Name = "cust")] Customer customer, string cust) => {}); |
I guess there isn't anything wrong with multiple arguments binding to the same route value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
It would be good to add a unit test of multiple arguments binding to one parameter, just to verify that it doesn't break the analyzer. Maybe a comment to the code as well that it doesn't matter if multiple parameters have the same name.
...pNetCoreAnalyzers/src/Analyzers/RouteHandlers/DisallowNonParsableComplexTypesOnParameters.cs
Outdated
Show resolved
Hide resolved
Fixes in various bits of route tooling for this issue: #45720 My PR adds the resolved parameter name to route usage information. I reused and centralized what you did in this PR. It should be possible to simplify this PR by reusing it. |
Will pick this up after the holidays and integrate your changes (assuming they are merged by then). |
0595940
to
7cbe4b3
Compare
06ece9a
to
a6a69a8
Compare
@JamesNK I integrated your changes into my PR. It involved moving the Wanted to give you a final chance to review before I squash and merge. |
I made a collection of resolved parameters available from Lines 25 to 44 in 45fcfb7
|
Are you suggesting something like this: var parameterSymbol = routeUsage.UsageContext.ResolvedParameters.FirstOrDefault(p => p.Symbol == (ISymbol)handlerDelegateParameter);
var routeParameterName = parameterSymbol.RouteParameterName ?? handlerDelegateParameter.Name;
// Match handler parameter against route parameters. If it is a route parameter it needs to be parsable/bindable in some fashion.
if (routeUsage.RoutePattern.TryGetRouteParameter(routeParameterName, out var routeParameter))
{
... If that is the case, it doesn't seem to work. I can never get ... I guess I can just resolve on symbol name in this particular case, but it's troubling that these objects don't |
…aming.
Detect parsability even when
FromRoute
attribute is used to map handler parameters to route parameters.There was a bug in my recent analyzer change where the following wouldn't produce an error:
It should however warn that
Customer
type is not parsable.Description
To fix I introduced some logic to resolve the route parameter name if the
FromRoute
attribute is present. If it is it uses that instead to determine whether to apply the analyzer error. Arguably I could key just off the presence of the[FromRoute]
attribute and not drill into the name, but I think we would want to avoid providing this analyzer error in that case and allow another analyzer to warn on a mismatch between route parameters and handler parameters.Fixes #45581