Skip to content
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(router): ignore null or undefined query parameters #12333

Merged
merged 1 commit into from
Nov 10, 2016
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
13 changes: 13 additions & 0 deletions modules/@angular/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ export class Router {
*/
navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
Promise<boolean> {
if (typeof extras.queryParams === 'object' && extras.queryParams !== null) {
extras.queryParams = this.removeEmptyProps(extras.queryParams);
}
return this.navigateByUrl(this.createUrlTree(commands, extras), extras);
}

Expand All @@ -549,6 +552,16 @@ export class Router {
}
}

private removeEmptyProps(params: Params): Params {
return Object.keys(params).reduce((result: Params, key: string) => {
const value: any = params[key];
if (value !== null && value !== undefined) {
result[key] = value;
}
return result;
}, {});
}

private processNavigations(): void {
concatMap
.call(
Expand Down
30 changes: 27 additions & 3 deletions modules/@angular/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ describe('Integration', () => {
expect(fixture.nativeElement).toHaveText('query: 2 fragment: fragment2');
})));

it('should ignore null and undefined query params',
fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp);

router.resetConfig([{path: 'query', component: EmptyQueryParamsCmp}]);

router.navigate(['query'], {queryParams: {name: 1, age: null, page: undefined}});
advance(fixture);
const cmp = fixture.debugElement.children[1].componentInstance;
expect(cmp.recordedParams).toEqual([{name: '1'}]);
})));

it('should push params only when they change', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp);

Expand Down Expand Up @@ -2416,6 +2428,15 @@ class QueryParamsAndFragmentCmp {
}
}

@Component({selector: 'empty-query-cmp', template: ``})
class EmptyQueryParamsCmp {
recordedParams: Params[] = [];

constructor(route: ActivatedRoute) {
route.queryParams.forEach(_ => this.recordedParams.push(_));
}
}

@Component({selector: 'route-cmp', template: `route`})
class RouteCmp {
constructor(public route: ActivatedRoute) {}
Expand Down Expand Up @@ -2509,7 +2530,8 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
RouteCmp,
RootCmp,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp
],


Expand All @@ -2534,7 +2556,8 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
RouteCmp,
RootCmp,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp
],


Expand All @@ -2560,7 +2583,8 @@ function createRoot(router: Router, type: any): ComponentFixture<any> {
RouteCmp,
RootCmp,
RelativeLinkInIfCmp,
RootCmpWithTwoOutlets
RootCmpWithTwoOutlets,
EmptyQueryParamsCmp
]
})
class TestModule {
Expand Down