Skip to content

Commit

Permalink
fix(router): always stringify matrix parameters (#25095)
Browse files Browse the repository at this point in the history
Fix a case where matrix parameters weren't stringified when they are passed as a first command
when creating a url tree. Fix return type in parseMatrixParams method
because it always returns {[key: string]: string}

Closes #23165

PR Close #25095
  • Loading branch information
AlexMokin authored and thePunderWoman committed Jan 21, 2021
1 parent 51d6aed commit 3cf4e3c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/router/src/create_url_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function createNewSegmentGroup(
// if we start with an object literal, we need to reuse the path part from the segment
if (i === 0 && isMatrixParams(commands[0])) {
const p = segmentGroup.segments[startIndex];
paths.push(new UrlSegment(p.path, commands[0]));
paths.push(new UrlSegment(p.path, stringify(commands[0])));
i++;
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/router/src/url_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,15 @@ class UrlParser {
return new UrlSegment(decode(path), this.parseMatrixParams());
}

private parseMatrixParams(): {[key: string]: any} {
const params: {[key: string]: any} = {};
private parseMatrixParams(): {[key: string]: string} {
const params: {[key: string]: string} = {};
while (this.consumeOptional(';')) {
this.parseParam(params);
}
return params;
}

private parseParam(params: {[key: string]: any}): void {
private parseParam(params: {[key: string]: string}): void {
const key = matchSegments(this.remaining);
if (!key) {
return;
Expand Down
12 changes: 12 additions & 0 deletions packages/router/test/create_url_tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ describe('createUrlTree', () => {
expect(serializer.serialize(t)).toEqual('/a/b;aa=22;bb=33');
});

it('should stringify matrix parameters', () => {
const pr = serializer.parse('/r');
const relative = create(pr.root.children[PRIMARY_OUTLET], 0, pr, [{pp: 22}]);
const segmentR = relative.root.children[PRIMARY_OUTLET].segments[0];
expect(segmentR.parameterMap.get('pp')).toEqual('22');

const pa = serializer.parse('/a');
const absolute = createRoot(pa, ['/b', {pp: 33}]);
const segmentA = absolute.root.children[PRIMARY_OUTLET].segments[0];
expect(segmentA.parameterMap.get('pp')).toEqual('33');
});

describe('relative navigation', () => {
it('should work', () => {
const p = serializer.parse('/a/(c//left:cp)(left:ap)');
Expand Down

0 comments on commit 3cf4e3c

Please sign in to comment.