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): scoped injection with symbol field name #9456

Merged

Conversation

MyAeroCode
Copy link
Contributor

@MyAeroCode MyAeroCode commented Apr 10, 2022

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?

Issue Number: #9352

Symbol filed name issue:

Some methods did not take the symbol type as keyOrIndex.
For example, here is the main code that causing this issue:

  protected addDependencyMetadata(
    keyOrIndex: number | string,  // <- actually, number | string | symbol
    hostWrapper: InstanceWrapper,
    instanceWrapper: InstanceWrapper,
  ) {
    isString(keyOrIndex)
      ? hostWrapper.addPropertiesMetadata(keyOrIndex, instanceWrapper)
      : hostWrapper.addCtorMetadata(keyOrIndex, instanceWrapper); // <- symbol key reach this code
  }

Symbol injection key issue:

NestApplicationContext doesn't resolve with symbol injection key when scope is REQUEST.

error message:

    TypeError: Cannot read property 'getInstanceByContextId' of undefined

      at Injector.loadInstance (node_modules/@nestjs/core/injector/injector.js:32:38)
      at Injector.loadPerContext (node_modules/@nestjs/core/injector/injector.js:344:20)
      at TestingModule.resolvePerContext (node_modules/@nestjs/core/nest-application-context.js:240:46)
      at TestingModule.resolve (node_modules/@nestjs/core/nest-application-context.js:60:21)

Injector#loadPerContext expect wrapper argument is not undefined.

  public async loadPerContext<T = any>(
    instance: T,
    moduleRef: Module,
    collection: Map<InstanceToken, InstanceWrapper>,
    ctx: ContextId,
    wrapper?: InstanceWrapper, // <- actually, is undefined
  ): Promise<T> {
    if (!wrapper) { // <- is true
      const injectionToken = instance.constructor; //  assign to ClassConstructor

      // should be collection.get(symbol)
      // not collection.get(constructor)
      wrapper = collection.get(injectionToken); // is undefined
    }
    await this.loadInstance(wrapper, collection, moduleRef, ctx, wrapper);
    await this.loadEnhancersPerContext(wrapper, ctx, wrapper);

    const host = wrapper.getInstanceByContextId(ctx, wrapper.id);
    return host && (host.instance as T);
  }

What is the new behavior?

Symbol filed name issue:

Add symbol type into all keyOrIndex argument.

  protected addDependencyMetadata(
    keyOrIndex: symbol | string | number, // <- symbol type added
    hostWrapper: InstanceWrapper,
    instanceWrapper: InstanceWrapper,
  ) {
    if (isSymbol(keyOrIndex) || isString(keyOrIndex)) {
      hostWrapper.addPropertiesMetadata(keyOrIndex, instanceWrapper);
    } else {
      hostWrapper.addCtorMetadata(keyOrIndex, instanceWrapper);
    }
  }

Symbol injection key issue:

Pass wrapper to Injector.loadPerContext

  protected async resolvePerContext<TInput = any, TResult = TInput>(
    typeOrToken: Type<TInput> | Abstract<TInput> | string | symbol,
    contextModule: Module,
    contextId: ContextId,
    options?: { strict: boolean },
  ): Promise<TResult> {
    const isStrictModeEnabled = options && options.strict;
    const instanceLink = isStrictModeEnabled
      ? this.instanceLinksHost.get(typeOrToken, contextModule.id)
      : this.instanceLinksHost.get(typeOrToken);
    const { wrapperRef, collection } = instanceLink;
    if (wrapperRef.isDependencyTreeStatic() && !wrapperRef.isTransient) {
      return this.get(typeOrToken, options);
    }
    const ctorHost = wrapperRef.instance || { constructor: typeOrToken };
    const instance = await this.injector.loadPerContext(
      ctorHost,
      wrapperRef.host,
      collection,
      contextId,
      wrapperRef, // <- new line
    );

    // ...
}

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@coveralls
Copy link

Pull Request Test Coverage Report for Build ca2ead91-e2ff-4ac0-87ac-36003bfe03c5

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.07%) to 94.111%

Totals Coverage Status
Change from base Build 680b7198-9de4-4879-a777-5f22f2cd17bf: 0.07%
Covered Lines: 5769
Relevant Lines: 6130

💛 - Coveralls

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.

None yet

3 participants