Skip to content

fix(angular): Handle routes with empty path #7686

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

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}/` : '/';
}
26 changes: 25 additions & 1 deletion packages/angular/test/tracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ describe('Angular Tracing', () => {

describe('getParameterizedRouteFromSnapshot', () => {
it.each([
['returns `/` empty object if the route no children', {}, '/'],
['returns `/` if the route has no children', {}, '/'],
[
'returns `/` if the route has an 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