Skip to content

Commit bfa53cd

Browse files
atscottcrisbeto
authored andcommitted
fix(router): handle parenthesized outlets without a name in DefaultUrlSerializer (#64507)
Previously, the `DefaultUrlSerializer` would incorrectly parse URLs with a parenthesized outlet that did not have a name, such as `/(left)`. This would result in an `undefined` outlet name in the serialized URL. This commit fixes the issue by ensuring that parenthesized outlets without a name are treated as primary outlets. fixes #58516. Based on the description, either the URL was constructed manually or by custom serializer. PR Close #64507
1 parent 2a249b7 commit bfa53cd

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/router/src/url_tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ class UrlParser {
717717
);
718718
}
719719

720-
let outletName: string = undefined!;
720+
let outletName: string | undefined;
721721
if (path.indexOf(':') > -1) {
722722
outletName = path.slice(0, path.indexOf(':'));
723723
this.capture(outletName);
@@ -727,7 +727,7 @@ class UrlParser {
727727
}
728728

729729
const children = this.parseChildren();
730-
segments[outletName] =
730+
segments[outletName ?? PRIMARY_OUTLET] =
731731
Object.keys(children).length === 1 && children[PRIMARY_OUTLET]
732732
? children[PRIMARY_OUTLET]
733733
: new UrlSegmentGroup([], children);

packages/router/test/url_tree.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ describe('UrlTree', () => {
4040
const p = router.parseUrl(serialized);
4141
expect(router.serializeUrl(p)).toBe(serialized);
4242
});
43+
44+
it('should work with named outlet with primary and immediate named siblings', () => {
45+
const router = TestBed.inject(Router);
46+
const tree = router.createUrlTree([
47+
{
48+
outlets: {
49+
primary: ['Home'],
50+
app: ['Welcome'],
51+
dock: [{outlets: {primary: 'left', 1: ['One', {pinned: true}]}}],
52+
},
53+
},
54+
]);
55+
const url = tree.toString();
56+
expect(url).toBe('/Home(app:Welcome//dock:/(left//1:One;pinned=true))');
57+
const tree2 = serializer.parse(url);
58+
expect(serializer.serialize(tree2)).toBe(url);
59+
});
4360
});
4461

4562
describe('containsTree', () => {

0 commit comments

Comments
 (0)