-
Notifications
You must be signed in to change notification settings - Fork 282
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 multiple routes of same template in attribute-based routing #2250
base: main
Are you sure you want to change the base?
Fix multiple routes of same template in attribute-based routing #2250
Conversation
Please sign EasyCLA. It is hard requirement to accept any contribution to this repository. |
Thanks. Working with my company to figure this out. |
|
Okay, I finally got my company to sign the CLA. Please proceed with reviewing! |
This PR was marked stale due to lack of activity. It will be closed in 7 days. |
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.
Add an entry to the Changelog
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.
Update the PR title to start with [Instrumentation.AspNet]
routeData = new RouteData(); | ||
var multiMethodSubroutes = new[] | ||
{ | ||
new | ||
{ | ||
Route = new | ||
{ | ||
RouteTemplate = routeTemplate, | ||
DataTokens = new Dictionary<string, object> | ||
{ | ||
["actions"] = new[] | ||
{ | ||
new | ||
{ | ||
SupportedHttpMethods = new[] | ||
{ | ||
HttpMethod.Get, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
new | ||
{ | ||
Route = new | ||
{ | ||
RouteTemplate = routeTemplate, | ||
DataTokens = new Dictionary<string, object> | ||
{ | ||
["actions"] = new[] | ||
{ | ||
new | ||
{ | ||
SupportedHttpMethods = new[] | ||
{ | ||
HttpMethod.Put, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
new | ||
{ | ||
Route = new | ||
{ | ||
RouteTemplate = routeTemplate, | ||
DataTokens = new Dictionary<string, object> | ||
{ | ||
["actions"] = new[] | ||
{ | ||
new | ||
{ | ||
SupportedHttpMethods = new[] | ||
{ | ||
HttpMethod.Delete, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
routeData.Values.Add( | ||
"MS_SubRoutes", | ||
multiMethodSubroutes); |
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.
I think we can simplify to:
routeData = new RouteData();
var httpMethods = new[] { HttpMethod.Get, HttpMethod.Put, HttpMethod.Delete };
var multiMethodSubroutes = httpMethods.Select(method => new
{
Route = new
{
RouteTemplate = routeTemplate,
DataTokens = new Dictionary<string, object>
{
["actions"] = new[]
{
new
{
SupportedHttpMethods = new[] { method }
}
}
}
}
}).ToArray();
routeData.Values.Add("MS_SubRoutes", multiMethodSubroutes);
Changes
ASP.NET instrumentation fails to extract route for attribute-based routing, if multiple HTTP methods use the same route template--a common scenario for RESTful APIs. For example:
In this case, the
MS_Subroutes
array will have multiple elements, each corresponding to a different HTTP method.The original code only extracts the route template if there's only one element in the array. The correct thing to do is to just extract the template from the first subroute, since all subroutes have the same template.
It's not possible for MS_Subroutes array to have different templates in its elements. WebAPI would fail to route and throw an exception, if you declare multiple actions for the same method/template
Merge requirement checklist
CHANGELOG.md
files updated for non-trivial changes