Skip to content

fix(router): drop matrix parameters with null or undefined values - #72207

Open
phr4nky wants to merge 2 commits into
angular:mainfrom
phr4nky:fix/router-matrix-params-null-undefined
Open

phr4nky wants to merge 2 commits into
angular:mainfrom
phr4nky:fix/router-matrix-params-null-undefined

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

Matrix parameters with null or undefined values are serialized as the literal strings "null" and "undefined":

router.navigate(['/products', {category: undefined, page: null}]);
// URL: /products;category=undefined;page=null
// route.snapshot.paramMap.get('category') === 'undefined'

The same values passed as queryParams are removed (/products, covered by should remove query params with null or undefined values). With optional matrix parameters, as the routing guide describes them, an unset value therefore shows up in the URL and reaches the component as a non-empty string.

Issue Number: #72208

What is the new behavior?

Matrix parameters with null or undefined values are omitted, consistent with query parameters. Setting an existing matrix parameter to null removes it from the segment.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

The new test fails without the change (Expected '/a;pp=null;dd=undefined;ee=33' to equal '/a;ee=33') and //packages/router/test:test passes with it.

Matrix parameters passed to `createUrlTree`, `navigate` or `routerLink`
were stringified unconditionally, so `['/products', {category: undefined}]`
produced `/products;category=undefined` and `paramMap.get('category')`
returned the string `'undefined'`. Query parameters with `null` or
`undefined` values are already removed; matrix parameters now behave
the same way.

@atscott atscott left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Could we also update prefixedWith to pass stringify(next) to compare (and add a test for it)?

Right now prefixedWith passes the raw next object to compare(curr, next, path), which checks shallowEqual(params, segment.parameters). If the current URL is something like /a/(b//right:c) and a command passes ['/a', {pp: undefined}, 'd'] or ['/a', {pp: undefined}, {outlets: {right: 'd'}}], stringify will drop pp so the segment matches /a, but compare still sees the pp key on next and returns false. That causes updateSegmentGroup to fall back to createNewSegmentGroup and drop the existing child/secondary outlets.

@phr4nky

phr4nky commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Done in 2f2e3e0: prefixedWith now passes stringify(next) to compare, with a test covering both of your examples against /a/(b//right:c):

  • ['/a', {pp: undefined}, 'd'] → /a/(d//right:c)
  • ['/a', {pp: null}, {outlets: {right: 'd'}}] → /a/(b//right:d)

Without the source change the test produces /a/d and /a/(right:d).

One side effect worth flagging: since the params are stringified before comparing, a command like {pp: 11} now also matches an existing ;pp=11 segment, where it previously did not. That seems consistent with how the params end up in the tree, but let me know if you would rather keep that out of this PR.

I pushed it as a fixup commit; happy to squash it if you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants