Skip to content

Commit 029d0f2

Browse files
vicbchuckjaz
authored andcommitted
fix(router): fix query parameters with multiple values (angular#15129)
fixes angular#14796
1 parent 3f38c6f commit 029d0f2

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

packages/router/src/create_url_tree.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ function isMatrixParams(command: any): boolean {
4040
function tree(
4141
oldSegmentGroup: UrlSegmentGroup, newSegmentGroup: UrlSegmentGroup, urlTree: UrlTree,
4242
queryParams: Params, fragment: string): UrlTree {
43+
let qp: any = {};
44+
if (queryParams) {
45+
forEach(queryParams, (value: any, name: any) => {
46+
qp[name] = Array.isArray(value) ? value.map((v: any) => `${v}`) : `${value}`;
47+
});
48+
}
49+
4350
if (urlTree.root === oldSegmentGroup) {
44-
return new UrlTree(newSegmentGroup, stringify(queryParams), fragment);
51+
return new UrlTree(newSegmentGroup, qp, fragment);
4552
}
4653

47-
return new UrlTree(
48-
replaceSegment(urlTree.root, oldSegmentGroup, newSegmentGroup), stringify(queryParams),
49-
fragment);
54+
return new UrlTree(replaceSegment(urlTree.root, oldSegmentGroup, newSegmentGroup), qp, fragment);
5055
}
5156

5257
function replaceSegment(

packages/router/test/create_url_tree.spec.ts

+24-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ import {DefaultUrlSerializer, UrlSegmentGroup, UrlTree} from '../src/url_tree';
1616
describe('createUrlTree', () => {
1717
const serializer = new DefaultUrlSerializer();
1818

19+
describe('query parameters', () => {
20+
it('should support parameter with multiple values', () => {
21+
const p1 = serializer.parse('/');
22+
const t1 = createRoot(p1, ['/'], {m: ['v1', 'v2']});
23+
expect(serializer.serialize(t1)).toEqual('/?m=v1&m=v2');
24+
25+
const p2 = serializer.parse('/a/c');
26+
const t2 = create(p2.root.children[PRIMARY_OUTLET], 1, p2, ['c2'], {m: ['v1', 'v2']});
27+
expect(serializer.serialize(t2)).toEqual('/a/c/c2?m=v1&m=v2');
28+
});
29+
30+
it('should set query params', () => {
31+
const p = serializer.parse('/');
32+
const t = createRoot(p, [], {a: 'hey'});
33+
expect(t.queryParams).toEqual({a: 'hey'});
34+
});
35+
36+
it('should stringify query params', () => {
37+
const p = serializer.parse('/');
38+
const t = createRoot(p, [], <any>{a: 1});
39+
expect(t.queryParams).toEqual({a: '1'});
40+
});
41+
});
42+
1943
it('should navigate to the root', () => {
2044
const p = serializer.parse('/');
2145
const t = createRoot(p, ['/']);
@@ -209,18 +233,6 @@ describe('createUrlTree', () => {
209233
});
210234
});
211235

212-
it('should set query params', () => {
213-
const p = serializer.parse('/');
214-
const t = createRoot(p, [], {a: 'hey'});
215-
expect(t.queryParams).toEqual({a: 'hey'});
216-
});
217-
218-
it('should stringify query params', () => {
219-
const p = serializer.parse('/');
220-
const t = createRoot(p, [], <any>{a: 1});
221-
expect(t.queryParams).toEqual({a: '1'});
222-
});
223-
224236
it('should set fragment', () => {
225237
const p = serializer.parse('/');
226238
const t = createRoot(p, [], {}, 'fragment');

packages/router/test/url_serializer.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ describe('url serializer', () => {
163163
it('should handle multiple query params of the same name into an array', () => {
164164
const tree = url.parse('/one?a=foo&a=bar&a=swaz');
165165
expect(tree.queryParams).toEqual({a: ['foo', 'bar', 'swaz']});
166+
expect(url.serialize(tree)).toEqual('/one?a=foo&a=bar&a=swaz');
166167
});
167168

168169
it('should parse fragment', () => {

0 commit comments

Comments
 (0)