Skip to content

Commit

Permalink
fix(angular): Handle routes with empty path
Browse files Browse the repository at this point in the history
Update function getParameterizedRouteFromSnapshot to handle routes with empty paths.

Fixes GH-7681
  • Loading branch information
ddontsov-b2broker committed Mar 31, 2023
1 parent 1c009fa commit a7f013a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/angular/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,24 @@ export function TraceMethodDecorator(): MethodDecorator {
* child route with its parent to produce the complete parameterized URL of the activated route.
* This happens recursively until the last child (i.e. the end of the URL) is reached.
*
* @param route the ActivatedRouteSnapshot of which its path and its child's path is concantenated
* @param route the ActivatedRouteSnapshot of which its path and its child's path is concatenated
*
* @returns the concatenated parameterzited route string
* @returns the concatenated parameterized route string
*/
export function getParameterizedRouteFromSnapshot(route?: ActivatedRouteSnapshot | null): string {
const path = route && route.firstChild && route.firstChild.routeConfig && route.firstChild.routeConfig.path;
if (!path) {
return '/';
const parts: string[] = [];

let currentRoute = route && route.firstChild;
while (currentRoute) {
const path = currentRoute && currentRoute.routeConfig && currentRoute.routeConfig.path;
if (path === null || path === undefined) {
break;
}

parts.push(path);
currentRoute = currentRoute.firstChild;
}
return `/${path}${getParameterizedRouteFromSnapshot(route && route.firstChild)}`;

const fullPath = parts.filter(part => part).join('/');
return fullPath ? `/${fullPath}/` : '/';
}
24 changes: 24 additions & 0 deletions packages/angular/test/tracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ describe('Angular Tracing', () => {
describe('getParameterizedRouteFromSnapshot', () => {
it.each([
['returns `/` empty object if the route no children', {}, '/'],
[
'returns `/` if the route with empty child',
{
firstChild: { routeConfig: { path: '' } },
},
'/',
],
[
'returns the route of a snapshot without children',
{
Expand All @@ -76,6 +83,21 @@ describe('Angular Tracing', () => {
},
'/orgs/:orgId/projects/:projId/overview/',
],
[
'returns the route of a snapshot without children but with empty paths',
{
firstChild: {
routeConfig: { path: 'users' },
firstChild: {
routeConfig: { path: '' },
firstChild: {
routeConfig: { path: ':id' },
},
},
},
},
'/users/:id/',
],
])('%s', (_, routeSnapshot, expectedParams) => {
expect(getParameterizedRouteFromSnapshot(routeSnapshot as unknown as ActivatedRouteSnapshot)).toEqual(
expectedParams,
Expand Down Expand Up @@ -345,6 +367,7 @@ describe('Angular Tracing', () => {
public ngOnInit() {
origNgOnInitMock();
}

public ngAfterViewInit() {
origNgAfterViewInitMock();
}
Expand Down Expand Up @@ -398,6 +421,7 @@ describe('Angular Tracing', () => {
public ngOnInit() {
origNgOnInitMock();
}

@TraceMethodDecorator()
public ngAfterViewInit() {
origNgAfterViewInitMock();
Expand Down

0 comments on commit a7f013a

Please sign in to comment.