Repository navigation
Conversation
Since 011bff6 (nestjs#2581) and ad18890 (nestjs#17838), @optional() stores constructor parameter metadata with getOwnMetadata and Injector reads it from getConstructorOwner(type), while nestjs#17949 fixed redeclared @Inject() properties. The constructor parameter branch of @Inject() and Injector#reflectSelfParams() still used Reflect.getMetadata(). As a result, a subclass that redeclared its constructor without @Inject() inherited the parent's SELF_DECLARED_DEPS_METADATA and had its parameters silently replaced at matching indices. A subclass that used @Inject() on its own constructor also copied the parent's entries into its own metadata array. Read SELF_DECLARED_DEPS_METADATA with getOwnMetadata in @Inject() and from getConstructorOwner(type) in Injector#reflectSelfParams() so a redeclared constructor uses only its own tokens while an inherited constructor still keeps the parent's tokens.
Member
|
Thanks for digging into this. Reading A subclass that redeclares its constructor and relies on the parent's With this PR, bootstrap fails with "Nest can't resolve dependencies of the Child (?, Other)". When the class at that position is itself a provider, the PR would inject that provider instead of the token, with no error at all. The equivalent change for |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Background & Context
This PR follows up on @kamilmysliwiec's work and design for constructor parameter metadata inheritance across class hierarchies:
@Optional()(011bff6601/ Remove optional dependency metadata inherited from base class #2581): @kamilmysliwiec updated@Optional()andInjector#reflectOptionalParams()to readOPTIONAL_DEPS_METADATAwithReflect.getOwnMetadata()so that a subclass redeclaring its constructor does not inherit the parent's optional parameter indices.ad1889073d/ fix(core): inherit optional ctor params when the ctor is not redeclared #17838): @kamilmysliwiec followed up by introducingInjector#getConstructorOwner(type)so that a subclass without its own constructor (e.g.,class LocalAuthGuard extends AuthGuard('local') {}or mixin subclasses) still inherits constructor metadata from the nearest class in the prototype chain that declares the constructor.Notably, when designing
getConstructorOwner(), @kamilmysliwiec already includedSELF_DECLARED_DEPS_METADATAas one of the three metadata keys identifying the constructor owner:@Optional()properties optional #17944 and fix(common): let a redeclared@Inject()property replace the parent's #17949): Property-based@Optional()and@Inject()inheritance (OPTIONAL_PROPERTY_DEPS_METADATAandPROPERTY_DEPS_METADATA) were subsequently aligned.However, the constructor parameter branch of
@Inject()(SELF_DECLARED_DEPS_METADATA) was missed in both places and still usesReflect.getMetadata():packages/common/decorators/core/inject.decorator.ts(line 52)packages/core/injector/injector.tsinInjector#reflectSelfParams()(line 696)Reproduction of the two failure modes
ChildWithoutInjectServicehas no ownSELF_DECLARED_DEPS_METADATA.Injector#reflectSelfParams()callsReflect.getMetadata(SELF_DECLARED_DEPS_METADATA, type), walks up the prototype chain toParentService, and silently replaces parameter index0(MyService) with'PARENT_TOKEN'. (Even an emptyconstructor()on the child ends up injecting'PARENT_TOKEN'at index0.)@Inject('CHILD_TOKEN')executes onChildWithInjectService,inject.decorator.tsreadsReflect.getMetadata(SELF_DECLARED_DEPS_METADATA, target), copies{ index: 0, param: 'PARENT_TOKEN' }fromParentServiceinto the child's own metadata array, and appends{ index: 1, param: 'CHILD_TOKEN' }. Again, parameter index0(MyService) is silently overwritten with'PARENT_TOKEN'.Issue Number: Related to #2581, #17838, #17949
What is the new behavior?
Completes @kamilmysliwiec's constructor-owner metadata design from
011bff6601(#2581) andad1889073d(#17838) for@Inject()constructor parameters:@Inject()(packages/common/decorators/core/inject.decorator.ts): UsesReflect.getOwnMetadata(SELF_DECLARED_DEPS_METADATA, target) || []in the parameter branch (!isUndefined(index)), matching@Optional()inoptional.decorator.ts.Injector#reflectSelfParams()(packages/core/injector/injector.ts): ResolvesSELF_DECLARED_DEPS_METADATAviaReflect.getOwnMetadata(SELF_DECLARED_DEPS_METADATA, this.getConstructorOwner(type)) || [], matchingInjector#reflectOptionalParams().getConstructorOwner(type)resolves to the parent class declaring the constructor, so the subclass continues to inherit the parent's@Inject()parameter tokens.getConstructorOwner(type)resolves to the subclass itself, so only the subclass's own@Inject()parameter tokens (if any) are used.Does this PR introduce a breaking change?
Other information
Unit tests have been added in:
packages/common/test/decorators/inject.decorator.spec.ts(verifying@Inject()on a subclass constructor parameter does not copy the parent'sSELF_DECLARED_DEPS_METADATA)packages/core/test/injector/injector.spec.ts(mirroring @kamilmysliwiec's@Optional()inheritance test suite from fix(core): inherit optional ctor params when the ctor is not redeclared #17838 for both inherited constructors acrossChildClass/GrandChildClassand redeclared constructors with@Inject(), without@Inject(), and with an emptyconstructor())