From cfd8ed3fff02af93b3fbd2e3f3a47128bd3582bf Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 17 Oct 2025 12:48:28 -0700 Subject: [PATCH] fix(router): Fix outlet serialization and parsing with no primary children (#64505) this fixes tree creation, serialization, and parsing of trees created with children outlets and no primary path fixes #62384 PR Close #64505 --- packages/router/src/url_tree.ts | 2 +- packages/router/test/url_tree.spec.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/router/src/url_tree.ts b/packages/router/src/url_tree.ts index 54cacd16f2c..b6d29898355 100644 --- a/packages/router/src/url_tree.ts +++ b/packages/router/src/url_tree.ts @@ -728,7 +728,7 @@ class UrlParser { const children = this.parseChildren(); segments[outletName] = - Object.keys(children).length === 1 + Object.keys(children).length === 1 && children[PRIMARY_OUTLET] ? children[PRIMARY_OUTLET] : new UrlSegmentGroup([], children); this.consumeOptional('//'); diff --git a/packages/router/test/url_tree.spec.ts b/packages/router/test/url_tree.spec.ts index 645e6c60b1b..10a37e0d469 100644 --- a/packages/router/test/url_tree.spec.ts +++ b/packages/router/test/url_tree.spec.ts @@ -6,7 +6,8 @@ * found in the LICENSE file at https://angular.dev/license */ -import {exactMatchOptions, subsetMatchOptions} from '../src/router'; +import {TestBed} from '@angular/core/testing'; +import {exactMatchOptions, Router, subsetMatchOptions} from '../src/router'; import {containsTree, DefaultUrlSerializer} from '../src/url_tree'; describe('UrlTree', () => { @@ -31,6 +32,14 @@ describe('UrlTree', () => { const tree = serializer.parse('/path/to?first=http://foo/bar?baz=true&second=123'); expect(tree.queryParams).toEqual({'first': 'http://foo/bar?baz=true', 'second': '123'}); }); + + it('create, serialize, parse, serialize results in same serialized tree with outlet and no primary children', () => { + const router = TestBed.inject(Router); + const th = router.createUrlTree(['/', {outlets: {a: ['a'], b: [{outlets: {a: ['b1']}}]}}]); + const serialized = router.serializeUrl(th); + const p = router.parseUrl(serialized); + expect(router.serializeUrl(p)).toBe(serialized); + }); }); describe('containsTree', () => {