diff --git a/packages/router/src/url_tree.ts b/packages/router/src/url_tree.ts index 6ed907e359..cd7d1b131e 100644 --- a/packages/router/src/url_tree.ts +++ b/packages/router/src/url_tree.ts @@ -497,12 +497,15 @@ function serializeMatrixParams(params: {[key: string]: string}): string { } function serializeQueryParams(params: {[key: string]: any}): string { - const strParams: string[] = Object.keys(params).map((name) => { - const value = params[name]; - return Array.isArray(value) ? - value.map(v => `${encodeUriQuery(name)}=${encodeUriQuery(v)}`).join('&') : - `${encodeUriQuery(name)}=${encodeUriQuery(value)}`; - }); + const strParams: string[] = + Object.keys(params) + .map((name) => { + const value = params[name]; + return Array.isArray(value) ? + value.map(v => `${encodeUriQuery(name)}=${encodeUriQuery(v)}`).join('&') : + `${encodeUriQuery(name)}=${encodeUriQuery(value)}`; + }) + .filter(s => !!s); return strParams.length ? `?${strParams.join('&')}` : ''; } diff --git a/packages/router/test/create_url_tree.spec.ts b/packages/router/test/create_url_tree.spec.ts index bf704d577d..8fd407906a 100644 --- a/packages/router/test/create_url_tree.spec.ts +++ b/packages/router/test/create_url_tree.spec.ts @@ -27,6 +27,16 @@ describe('createUrlTree', () => { expect(serializer.serialize(t2)).toEqual('/a/c/c2?m=v1&m=v2'); }); + it('should support parameter with empty arrays as values', () => { + const p1 = serializer.parse('/a/c'); + const t1 = create(p1.root.children[PRIMARY_OUTLET], 1, p1, ['c2'], {m: []}); + expect(serializer.serialize(t1)).toEqual('/a/c/c2'); + + const p2 = serializer.parse('/a/c'); + const t2 = create(p2.root.children[PRIMARY_OUTLET], 1, p2, ['c2'], {m: [], n: 1}); + expect(serializer.serialize(t2)).toEqual('/a/c/c2?n=1'); + }); + it('should set query params', () => { const p = serializer.parse('/'); const t = createRoot(p, [], {a: 'hey'});