From 3cf4e3c7c5f973f573576690a1c7e3f5b7019415 Mon Sep 17 00:00:00 2001 From: Alex Mokin Date: Wed, 25 Jul 2018 02:20:34 +0300 Subject: [PATCH] fix(router): always stringify matrix parameters (#25095) 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 --- packages/router/src/create_url_tree.ts | 2 +- packages/router/src/url_tree.ts | 6 +++--- packages/router/test/create_url_tree.spec.ts | 12 ++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/router/src/create_url_tree.ts b/packages/router/src/create_url_tree.ts index 2d2a9ae7ea50c..f70887abc3437 100644 --- a/packages/router/src/create_url_tree.ts +++ b/packages/router/src/create_url_tree.ts @@ -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; } diff --git a/packages/router/src/url_tree.ts b/packages/router/src/url_tree.ts index 66b90d61baa67..8d943ab841cee 100644 --- a/packages/router/src/url_tree.ts +++ b/packages/router/src/url_tree.ts @@ -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; diff --git a/packages/router/test/create_url_tree.spec.ts b/packages/router/test/create_url_tree.spec.ts index 0826b2c6ff96a..2d6a0dd3ece5a 100644 --- a/packages/router/test/create_url_tree.spec.ts +++ b/packages/router/test/create_url_tree.spec.ts @@ -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)');