Skip to content

Commit 61ba223

Browse files
Dzmitry Shylovichmatsko
authored andcommitted
fix(router): throw an error when navigate to null/undefined path
Closes #10560 Fixes #13384
1 parent 6164eb2 commit 61ba223

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

modules/@angular/router/src/router.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ export class Router {
526526
*/
527527
navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
528528
Promise<boolean> {
529+
validateCommands(commands);
529530
if (typeof extras.queryParams === 'object' && extras.queryParams !== null) {
530531
extras.queryParams = this.removeEmptyProps(extras.queryParams);
531532
}
@@ -1237,3 +1238,12 @@ function getOutlet(outletMap: RouterOutletMap, route: ActivatedRoute): RouterOut
12371238
}
12381239
return outlet;
12391240
}
1241+
1242+
function validateCommands(commands: string[]): void {
1243+
for (let i = 0; i < commands.length; i++) {
1244+
const cmd = commands[i];
1245+
if (cmd == null) {
1246+
throw new Error(`The requested path contains ${cmd} segment at index ${i}`);
1247+
}
1248+
}
1249+
}

modules/@angular/router/test/integration.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,17 @@ describe('Integration', () => {
536536
expect(cmp.recordedParams).toEqual([{name: '1'}]);
537537
})));
538538

539+
it('should throw an error when one of the commands is null/undefined',
540+
fakeAsync(inject([Router], (router: Router) => {
541+
createRoot(router, RootCmp);
542+
543+
router.resetConfig([{path: 'query', component: EmptyQueryParamsCmp}]);
544+
545+
expect(() => router.navigate([
546+
undefined, 'query'
547+
])).toThrowError(`The requested path contains undefined segment at index 0`);
548+
})));
549+
539550
it('should push params only when they change', fakeAsync(inject([Router], (router: Router) => {
540551
const fixture = createRoot(router, RootCmp);
541552

0 commit comments

Comments
 (0)