Skip to content

Commit 776d9ce

Browse files
authored
fix(angular): Handle routes with empty path (#7686)
Update function `getParameterizedRouteFromSnapshot` to handle routes with empty paths. Fixes #7681
1 parent e99492a commit 776d9ce

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

packages/angular/src/tracing.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,24 @@ export function TraceMethodDecorator(): MethodDecorator {
282282
* child route with its parent to produce the complete parameterized URL of the activated route.
283283
* This happens recursively until the last child (i.e. the end of the URL) is reached.
284284
*
285-
* @param route the ActivatedRouteSnapshot of which its path and its child's path is concantenated
285+
* @param route the ActivatedRouteSnapshot of which its path and its child's path is concatenated
286286
*
287-
* @returns the concatenated parameterzited route string
287+
* @returns the concatenated parameterized route string
288288
*/
289289
export function getParameterizedRouteFromSnapshot(route?: ActivatedRouteSnapshot | null): string {
290-
const path = route && route.firstChild && route.firstChild.routeConfig && route.firstChild.routeConfig.path;
291-
if (!path) {
292-
return '/';
290+
const parts: string[] = [];
291+
292+
let currentRoute = route && route.firstChild;
293+
while (currentRoute) {
294+
const path = currentRoute && currentRoute.routeConfig && currentRoute.routeConfig.path;
295+
if (path === null || path === undefined) {
296+
break;
297+
}
298+
299+
parts.push(path);
300+
currentRoute = currentRoute.firstChild;
293301
}
294-
return `/${path}${getParameterizedRouteFromSnapshot(route && route.firstChild)}`;
302+
303+
const fullPath = parts.filter(part => part).join('/');
304+
return fullPath ? `/${fullPath}/` : '/';
295305
}

packages/angular/test/tracing.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ describe('Angular Tracing', () => {
5555

5656
describe('getParameterizedRouteFromSnapshot', () => {
5757
it.each([
58-
['returns `/` empty object if the route no children', {}, '/'],
58+
['returns `/` if the route has no children', {}, '/'],
59+
[
60+
'returns `/` if the route has an empty child',
61+
{
62+
firstChild: { routeConfig: { path: '' } },
63+
},
64+
'/',
65+
],
5966
[
6067
'returns the route of a snapshot without children',
6168
{
@@ -76,6 +83,21 @@ describe('Angular Tracing', () => {
7683
},
7784
'/orgs/:orgId/projects/:projId/overview/',
7885
],
86+
[
87+
'returns the route of a snapshot without children but with empty paths',
88+
{
89+
firstChild: {
90+
routeConfig: { path: 'users' },
91+
firstChild: {
92+
routeConfig: { path: '' },
93+
firstChild: {
94+
routeConfig: { path: ':id' },
95+
},
96+
},
97+
},
98+
},
99+
'/users/:id/',
100+
],
79101
])('%s', (_, routeSnapshot, expectedParams) => {
80102
expect(getParameterizedRouteFromSnapshot(routeSnapshot as unknown as ActivatedRouteSnapshot)).toEqual(
81103
expectedParams,
@@ -345,6 +367,7 @@ describe('Angular Tracing', () => {
345367
public ngOnInit() {
346368
origNgOnInitMock();
347369
}
370+
348371
public ngAfterViewInit() {
349372
origNgAfterViewInitMock();
350373
}
@@ -398,6 +421,7 @@ describe('Angular Tracing', () => {
398421
public ngOnInit() {
399422
origNgOnInitMock();
400423
}
424+
401425
@TraceMethodDecorator()
402426
public ngAfterViewInit() {
403427
origNgAfterViewInitMock();

0 commit comments

Comments
 (0)