Fix property injection prototype hoisting bug
This commit is contained in:
9
src/di/decorator/getPropertyInjectionMetadata.ts
Normal file
9
src/di/decorator/getPropertyInjectionMetadata.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import {Instantiable, PropertyDependency} from '../types'
|
||||
import {Collection, logIfDebugging} from '../../util'
|
||||
import {propertyInjectionMetadata} from './propertyInjectionMetadata'
|
||||
|
||||
export function getPropertyInjectionMetadata(token: Instantiable<any>): Collection<PropertyDependency> {
|
||||
const loadedMeta = ((token as any)[propertyInjectionMetadata] || new Collection()) as Collection<PropertyDependency>
|
||||
logIfDebugging('extollo.di.injection', 'getPropertyInjectionMetadata() target:', token, 'loaded:', loadedMeta)
|
||||
return loadedMeta
|
||||
}
|
||||
@@ -2,16 +2,16 @@ import 'reflect-metadata'
|
||||
import {collect, Collection} from '../../util'
|
||||
import {logIfDebugging} from '../../util/support/debug'
|
||||
import {
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_SERVICE_TYPE_KEY,
|
||||
DependencyKey,
|
||||
DependencyRequirement,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY,
|
||||
isInstantiable,
|
||||
InjectionType,
|
||||
DEPENDENCY_KEYS_SERVICE_TYPE_KEY,
|
||||
isInstantiable,
|
||||
PropertyDependency,
|
||||
} from '../types'
|
||||
import {ContainerBlueprint} from '../ContainerBlueprint'
|
||||
import {propertyInjectionMetadata} from './propertyInjectionMetadata'
|
||||
|
||||
/**
|
||||
* Get a collection of dependency requirements for the given target object.
|
||||
@@ -67,6 +67,7 @@ export const Injectable = (): ClassDecorator => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mark the given class property to be injected by the container.
|
||||
* If a `key` is specified, that DependencyKey will be injected.
|
||||
@@ -77,10 +78,27 @@ export const Injectable = (): ClassDecorator => {
|
||||
*/
|
||||
export const Inject = (key?: DependencyKey, { debug = false } = {}): PropertyDecorator => {
|
||||
return (target, property) => {
|
||||
let propertyMetadata = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, target?.constructor || target) as Collection<PropertyDependency>
|
||||
if ( !target?.constructor ) {
|
||||
logIfDebugging('extollo.di.decoration', '[DEBUG] @Inject(): target has no constructor', target)
|
||||
throw new Error('Unable to define property injection: target has no constructor. Enable `extollo.di.decoration` logging to debug')
|
||||
}
|
||||
|
||||
const propertyTarget = target.constructor
|
||||
|
||||
// let propertyMetadata = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, propertyTarget) as Collection<PropertyDependency>
|
||||
// Okay, this is a little fucky. We can't use Reflect's metadata capabilities because we need to write the metadata to
|
||||
// the constructor, not the `target`. Because Reflect is using the prototype to store data, defining a metadata key on the constructor
|
||||
// will define it for its parent constructors as well.
|
||||
// So, if you have class A, class B extends A, and class C extends A, the properties for B and C will be defined on A, causing
|
||||
// BOTH B and C's properties to be injected on any class extending A.
|
||||
// To get around this, we instead define a custom property on the constructor itself, then use hasOwnProperty to make sure we're not
|
||||
// getting the one for the parent class via the prototype chain.
|
||||
let propertyMetadata = Object.prototype.hasOwnProperty.call(propertyTarget, propertyInjectionMetadata) ?
|
||||
(propertyTarget as any)[propertyInjectionMetadata] as Collection<PropertyDependency> : undefined
|
||||
|
||||
if ( !propertyMetadata ) {
|
||||
propertyMetadata = new Collection<PropertyDependency>()
|
||||
Reflect.defineMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, propertyMetadata, target?.constructor || target)
|
||||
;(propertyTarget as any)[propertyInjectionMetadata] = propertyMetadata
|
||||
}
|
||||
|
||||
const type = Reflect.getMetadata('design:type', target, property)
|
||||
@@ -103,7 +121,7 @@ export const Inject = (key?: DependencyKey, { debug = false } = {}): PropertyDec
|
||||
|
||||
logIfDebugging('extollo.di.decoration', '[DEBUG] @Inject() - key:', key, 'property:', property, 'target:', target, 'target constructor:', target?.constructor, 'type:', type)
|
||||
|
||||
Reflect.defineMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, propertyMetadata, target?.constructor || target)
|
||||
;(propertyTarget as any)[propertyInjectionMetadata] = propertyMetadata
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
2
src/di/decorator/propertyInjectionMetadata.ts
Normal file
2
src/di/decorator/propertyInjectionMetadata.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
export const propertyInjectionMetadata = Symbol('@extollo/lib:propertyInjectionMetadata')
|
||||
@@ -1,13 +1,13 @@
|
||||
import {AbstractFactory} from './AbstractFactory'
|
||||
import {
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY,
|
||||
DependencyRequirement,
|
||||
Instantiable,
|
||||
PropertyDependency,
|
||||
} from '../types'
|
||||
import {Collection, logIfDebugging} from '../../util'
|
||||
import 'reflect-metadata'
|
||||
import {getPropertyInjectionMetadata} from '../decorator/getPropertyInjectionMetadata'
|
||||
|
||||
/**
|
||||
* Standard static-class factory. The token of this factory is a reference to a
|
||||
@@ -57,7 +57,7 @@ export class Factory<T> extends AbstractFactory<T> {
|
||||
let currentToken = this.token
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
logIfDebugging('extollo.di.injection', 'Factory.getInjectedProperties() target:', currentToken, 'loaded:', loadedMeta)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
|
||||
@@ -13,5 +13,6 @@ export * from './ScopedContainer'
|
||||
export * from './types'
|
||||
|
||||
export * from './decorator/injection'
|
||||
export * from './decorator/getPropertyInjectionMetadata'
|
||||
export * from './InjectionAware'
|
||||
export * from './constructable'
|
||||
|
||||
Reference in New Issue
Block a user