Skip to content

fix(core): execute middleware in topological order of module imports #11133

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions integration/hello-world/e2e/middleware-execute-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,47 @@ import { INestApplication, MiddlewareConsumer, Module } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';

const RETURN_VALUE_A = 'test_A';
const RETURN_VALUE_B = 'test_B';

@Module({
imports: [],
})
class ModuleA {
class ModuleC {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => {
res.send(RETURN_VALUE_A);
res.append('x-trace', 'module-c');
next();
})
.forRoutes('hello');
.forRoutes('*');
}
}

@Module({
imports: [ModuleA],
imports: [],
})
class ModuleB {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => {
res.send(RETURN_VALUE_B);
res.append('x-trace', 'module-b');
next();
})
.forRoutes('hello');
.forRoutes('*');
}
}
@Module({ imports: [ModuleB] })
class ModuleA {
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => {
res.append('x-trace', 'module-a');
next();
})
.forRoutes('*');
}
}

@Module({
imports: [ModuleB],
imports: [ModuleA, ModuleC],
})
class TestModule {}

Expand All @@ -46,13 +56,18 @@ describe('Middleware (execution order)', () => {
}).compile()
).createNestApplication();

app.use((req, res, next) => {
res.append('x-trace', 'global');
next();
});

await app.init();
});

it(`should execute middleware in topological order`, () => {
return request(app.getHttpServer())
.get('/hello')
.expect(200, RETURN_VALUE_B);
.get('/')
.expect('x-trace', 'global, module-a, module-b, module-c');
});

afterEach(async () => {
Expand Down
10 changes: 1 addition & 9 deletions packages/core/middleware/middleware-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,7 @@ export class MiddlewareModule<
}
};

const entriesSortedByDistance = [...configs.entries()].sort(
([moduleA], [moduleB]) => {
return (
this.container.getModuleByKey(moduleA).distance -
this.container.getModuleByKey(moduleB).distance
);
},
);
for (const [moduleRef, moduleConfigurations] of entriesSortedByDistance) {
for (const [moduleRef, moduleConfigurations] of configs.entries()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would introduce a major breaking change. Modules are supposed to be sorted by distance

await registerAllConfigs(moduleRef, [...moduleConfigurations]);
}
}
Expand Down