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(core): merge req context with tenant payload in the request instance #13923

Merged
merged 2 commits into from
Nov 25, 2024

Conversation

DylanVeldra
Copy link
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?

Doesn't create the request singleton with the contextId payload when the first request isn't to a durable tree.

Issue Number: #13477

What is the new behavior?

The payload generated by the contextIdFactory will be always merged to the request singleton, not matter if the the tree is durable or not.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I would like to know which tests should I updated, if needed.

Also the patch doesn't merge payload from the contextIdFactory in the request passed to canActivate method for AuthGuard and exception filter.

To patch the case above:

packages/core/router/router-explorer.ts lines 389 and 402

  public createRequestScopedHandler(
    instanceWrapper: InstanceWrapper,
    requestMethod: RequestMethod,
    moduleRef: Module,
    moduleKey: string,
    methodName: string,
  ) {
    const { instance } = instanceWrapper;
    const collection = moduleRef.controllers;

    const isTreeDurable = instanceWrapper.isDependencyTreeDurable();

    return async <TRequest extends Record<any, any>, TResponse>(
      req: TRequest,
      res: TResponse,
      next: () => void,
    ) => {
      try {
        const contextId = this.getContextId(req, isTreeDurable);
        const contextInstance = await this.injector.loadPerContext(
          instance,
          moduleRef,
          collection,
          contextId,
        );
        await this.createCallbackProxy(
          contextInstance,
          contextInstance[methodName],
          methodName,
          moduleKey,
          requestMethod,
          contextId,
          instanceWrapper.id,
        )(req, res, next);
      } catch (err) {
        let exceptionFilter = this.exceptionFiltersCache.get(
          instance[methodName],
        );
        if (!exceptionFilter) {
          exceptionFilter = this.exceptionsFilter.create(
            instance,
            instance[methodName],
            moduleKey,
          );
          this.exceptionFiltersCache.set(instance[methodName], exceptionFilter);
        }
        const host = new ExecutionContextHost([req, res, next]);
        exceptionFilter.next(err, host);
      }
    };
  }

..., instanceWrapper.id, )(req, res, next);

should be replace by

..., instanceWrapper.id, )(Object.assign(req, contextId.payload), res, next);

and

const host = new ExecutionContextHost([req, res, next]);

should be replace by

const host = new ExecutionContextHost([Object.assign(req, contextId.payload), res, next]);

@coveralls
Copy link

Pull Request Test Coverage Report for Build 46b8cd77-a4be-4993-97f8-2c6e291084c4

Details

  • 1 of 2 (50.0%) changed or added relevant lines in 2 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage remained the same at 91.944%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/router/router-explorer.ts 0 1 0.0%
Files with Coverage Reduction New Missed Lines %
packages/core/router/router-explorer.ts 1 83.2%
Totals Coverage Status
Change from base Build 87a971db-c498-4818-a0cf-5b83ddab31ba: 0.0%
Covered Lines: 6814
Relevant Lines: 7411

💛 - Coveralls

@kamilmysliwiec kamilmysliwiec merged commit 9286733 into nestjs:master Nov 25, 2024
4 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.

3 participants