Skip to content

Router serializes null/undefined matrix parameters as the strings "null" and "undefined" #72208

Backlog

Description

@phr4nky

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

Matrix parameters with null or undefined values end up in the URL as the literal strings "null" and "undefined", while the same values passed as query parameters are removed:

const routes: Routes = [{path: 'products', component: ProductsComponent}];

const category = undefined; // optional filter not chosen yet
const page = null;

await router.navigate(['/products', {category, page}]);
router.url;                                  // '/products;category=undefined;page=null'
route.snapshot.paramMap.get('category');     // 'undefined' (string)
route.snapshot.paramMap.has('category');     // true

await router.navigate(['/products'], {queryParams: {category, page}});
router.url;                                  // '/products'
route.snapshot.queryParamMap.has('category'); // false

The same applies to routerLink and createUrlTree, e.g. [routerLink]="['/products', {category: selectedCategory()}]".

The routing guide presents matrix parameters as optional route parameters, so passing an object whose optional fields are unset is a natural pattern. The component then receives a non-empty string, so checks like params.get('category') ?? 'all' no longer apply, and the URL shows ;category=undefined.

The cause is stringify() in packages/router/src/create_url_tree.ts, which converts every value with a template string. Query parameters skip null/undefined explicitly (see should remove query params with null or undefined values, and 2dd93cd which aligned createUrlTreeFromSnapshot with Router.createUrlTree).

I could not find documentation stating either behavior for matrix parameters, so this may be intentional. If it is not, I opened a PR that drops null/undefined matrix parameters the same way query parameters are dropped: #72207. Since the current behavior is old, it might count as a breaking change; I'm happy to adjust the PR accordingly.

Please provide a link to a minimal reproduction of the bug

Inline snippet above. Reproduced against a build of main at 568c48b.

Please provide the exception or error you saw

No exception. Resulting URL: /products;category=undefined;page=null

Please provide the environment you discovered this bug in (run ng version)

@angular/router built from main (568c48b796), Node.js 26.3.0

Anything else?

No response

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions