Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform-express) make check for existing middlewares work with Express 5 #14574

Merged
merged 1 commit into from
Feb 6, 2025

Conversation

luddwichr
Copy link
Contributor

Express 5 made the router public API again and renamed the field from app._router to app.router. This broke the detection mechanism whether a middleware named "jsonParser" or "urlencodedParser" is already re or not, because app._router no longer exists.

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?

The express adapter overwrites existing middlewares called jsonParser and urlencodedParser with its on parsers.

This regression was caused by updating from Express 4 to 5. Express 5 exposes the router via app.router, while Express 4 exposed it via app._router.

Issue Number: N/A - I didn't create an issue yet - let me know if you need that

What is the new behavior?

The express adapter respects existing middlewares called jsonParser and urlencodedParser and stops registering its own parser middleware.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

…with Express 5

Express 5 made the router public API again and renamed the field from app._router to app.router.
This broke the detection mechanism whether a middleware named "jsonParser" or "urlencodedParser"
is already re or not, because app._router no longer exists.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 66d2d166-8df4-4956-b8ca-640d2ca9090c

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 89.702%

Totals Coverage Status
Change from base Build 8ad93cf3-f5a1-4dd6-a743-5980f21f6a8c: 0.0%
Covered Lines: 7108
Relevant Lines: 7924

💛 - Coveralls

@kamilmysliwiec kamilmysliwiec merged commit f061706 into nestjs:master Feb 6, 2025
3 checks passed
@kamilmysliwiec
Copy link
Member

LGTM

@luddwichr
Copy link
Contributor Author

luddwichr commented Feb 13, 2025

I just realized that the fix is incomplete :( - I missed the first line of the condition, so basically my fix does not work at all right now... I will try to provide a PR tomorrow.

Maybe some testing of the adapter should be added to help avoid regressions? I will try to figure out if I can add some basic test for the covered case, it that is desired/easily possible. @kamilmysliwiec you have an opinion regarding that?

@kamilmysliwiec
Copy link
Member

@luddwichr that would be great!

@luddwichr luddwichr deleted the fix-express-router-access branch February 14, 2025 19:51
luddwichr added a commit to luddwichr/nest that referenced this pull request Feb 14, 2025
…Express 5

Express 5 made the router public API again and renamed the field from app._router to app.router.
This broke the detection mechanism whether a middleware named "jsonParser" or "urlencodedParser"
is already registered or not.
Unfortunately, nestjs#14574 only fixed the issue partially.
This commit now uses app.router everywhere.
To avoid future regressions a test was added to verify the expected behavior.
luddwichr added a commit to luddwichr/nest that referenced this pull request Feb 14, 2025
…Express 5

Express 5 made the router public API again and renamed the field from app._router to app.router.
This broke the detection mechanism whether a middleware named "jsonParser" or "urlencodedParser"
is already registered or not.
Unfortunately, nestjs#14574 only fixed the issue partially.
This commit now uses app.router everywhere.
To avoid future regressions a test was added to verify the expected behavior.
@luddwichr
Copy link
Contributor Author

@kamilmysliwiec I created #14640 with the fix and a new test. However, the pipeline failed for node > 18 and I have no clue why :(

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.

3 participants