Skip to content

fix(core): normalize slashes when mapping controllers for middleware - #18075

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
basteez:fix/core-routes-mapper-trailing-slash
Oct 12, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
basteez:fix/core-routes-mapper-trailing-slash

Conversation

@basteez

@basteez basteez commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

This is the standalone RoutesMapper fix mentioned as point 4 in the review of #18023.

When middleware is bound with forRoutes(SomeController), RoutesMapper builds each path by concatenating the module path, the controller path and the method path as they are. The router builds the same routes with trailing slashes stripped at every join, so the two disagree whenever a segment ends in a slash, and the middleware silently never runs for the route it was bound to. The route itself still answers, so nothing visibly fails.

Two ordinary setups hit this:

@Controller('tail/')
class TailController {
  @Get('child')
  child() {}
}
// route: /tail/child, middleware path: /tail//child
RouterModule.register([{ path: '/', module: CatsModule }]);
// normalizePath keeps "/" as the module path
// route: /cats, middleware path: //cats

In both cases consumer.apply(SomeMiddleware).forRoutes(TailController) (or CatsController) has no effect on master.

What is the new behavior?

RoutesMapper strips the trailing slash from the module path and the controller path before joining them, the same way RoutePathFactory does for the routes, so the middleware path matches the registered route again.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Unit tests in routes-mapper.spec.ts cover a controller path ending in a slash, a module path of /, and the two combined. The new middleware-controller-path.spec.ts e2e spec reproduces both setups above plus a controller path ending in a slash under a RouterModule path. All three e2e cases fail on master (the handler answers instead of the middleware) and pass with this change.

@kamilmysliwiec
kamilmysliwiec merged commit f1a4d30 into nestjs:master Oct 12, 2026
2 checks passed
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