Skip to content

fix(common): keep an optional leading group in normalize path - #18579

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
rafay247:fix/router-module-optional-path
Nov 19, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
rafay247:fix/router-module-optional-path

Conversation

@rafay247

Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

Issue Number: N/A

A RouterModule route whose path is an optional group never matches:

RouterModule.register([{ path: '{/:lang}', module: CatsModule }])

GET /cats -> 404, GET /en/cats -> 404 (only //cats matches). The same group on a controller, @Controller('{/:lang}/cats'), works.

RouterModule paths go through normalizePath(), which always prepends /, so {/:lang} becomes /{/:lang} and the slash is no longer optional. #14545 fixed this in addLeadingSlash(), which controller paths use, but not in normalizePath().

What is the new behavior?

normalizePath() leaves a leading {/ as it is, like addLeadingSlash(). /cats and /en/cats both match, for top-level and child routes.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Tests: unit tests for normalizePath(), and e2e tests in integration/http-adapters (express and node), which fail on master.

Fastify rejects a path that starts with {/ ("The first character of a path should be /"), as it already does for @Controller('{/:lang}'), so the e2e test covers express and node like the existing {/:locale} test.

normalizePath() prepended "/" to a path that starts with an optional
group, so RouterModule.register([{ path: '{/:lang}', ... }]) became
"/{/:lang}" and its routes answered 404 with and without the group.
addLeadingSlash() already leaves "{/" alone (nestjs#14545).
@kamilmysliwiec
kamilmysliwiec merged commit ea23081 into nestjs:master Nov 19, 2026
2 checks passed
@rafay247
rafay247 deleted the fix/router-module-optional-path branch November 19, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants