Skip to content

Commit

Permalink
fix(platform-express) respect existing parser middlewares when using …
Browse files Browse the repository at this point in the history
…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, #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.
  • Loading branch information
luddwichr committed Feb 14, 2025
1 parent cda26df commit eff8f15
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/platform-express/adapters/express-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ export class ExpressAdapter extends AbstractHttpAdapter<
private isMiddlewareApplied(name: string): boolean {
const app = this.getInstance();
return (
!!app._router &&
!!app.router &&
!!app.router.stack &&
isFunction(app.router.stack.filter) &&
app.router.stack.some(
Expand Down
46 changes: 46 additions & 0 deletions packages/platform-express/test/adapters/express-adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ExpressAdapter } from '@nestjs/platform-express';
import { expect } from 'chai';
import * as express from 'express';
import * as sinon from 'sinon';

afterEach(() => sinon.restore());

describe.only('ExpressAdapter', () => {
describe('registerParserMiddleware', () => {
it('should register the express built-in parsers for json and urlencoded payloads', () => {
const expressInstance = express();
const jsonParserInstance = express.json();
const urlencodedInstance = express.urlencoded();
const jsonParserSpy = sinon
.stub(express, 'json')
.returns(jsonParserInstance);
const urlencodedParserSpy = sinon
.stub(express, 'urlencoded')
.returns(urlencodedInstance);
const useSpy = sinon.spy(expressInstance, 'use');
const expressAdapter = new ExpressAdapter(expressInstance);

expressAdapter.registerParserMiddleware();

expect(useSpy.calledTwice).to.be.true;
expect(useSpy.calledWith(sinon.match.same(jsonParserInstance))).to.be
.true;
expect(useSpy.calledWith(sinon.match.same(urlencodedInstance))).to.be
.true;
expect(jsonParserSpy.calledOnceWith({})).to.be.true;
expect(urlencodedParserSpy.calledOnceWith({ extended: true })).to.be.true;
});

it('should not register default parsers if custom parsers have already been registered', () => {
const expressInstance = express();
expressInstance.use(function jsonParser() {});
expressInstance.use(function urlencodedParser() {});
const useSpy = sinon.spy(expressInstance, 'use');
const expressAdapter = new ExpressAdapter(expressInstance);

expressAdapter.registerParserMiddleware();

expect(useSpy.called).to.be.false;
});
});
});

0 comments on commit eff8f15

Please sign in to comment.