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

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

wants to merge 2 commits into from

Conversation

igrek8
Copy link

@igrek8 igrek8 commented Feb 18, 2023

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?

Middleware registration does not respect DI module imports.

Given:

  • Module A
    • Middleware A
    • Module B
      • Middleware B
  • Module C
    • Middleware C

Resolution:

  1. Middleware A
  2. Middleware C
  3. Middleware B

What is the new behavior?

Order of middleware registration is resolved by DI module imports as described by the community.

Resolution:

  1. Middleware A
  2. Middleware B
  3. Middleware C

Middleware will run in the order that the modules are added to the imports array.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@igrek8 igrek8 marked this pull request as ready for review February 18, 2023 10:12
},
);
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

@kamilmysliwiec
Copy link
Member

Resolution:

Middleware A
Middleware C
Middleware B

This is the expected behavior given that module C is closer to the root module than module B

@nestjs nestjs locked and limited conversation to collaborators Feb 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants