Fix property injection prototype hoisting bug
This commit is contained in:
parent
c966904418
commit
a173393697
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@extollo/lib",
|
||||
"version": "0.14.1",
|
||||
"version": "0.14.2",
|
||||
"description": "The framework library that lifts up your code.",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
|
||||
Instantiable, FactoryProducer, getPropertyInjectionMetadata,
|
||||
} from '../../di'
|
||||
import {Collection, ErrorWithContext} from '../../util'
|
||||
import {Config} from '../../service/Config'
|
||||
@ -43,7 +43,7 @@ export class AuthenticatableRepositoryFactory extends AbstractFactory<Authentica
|
||||
let currentToken = this.getAuthenticatableRepositoryClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
|
||||
Instantiable, FactoryProducer, getPropertyInjectionMetadata,
|
||||
} from '../../../di'
|
||||
import {Collection, ErrorWithContext} from '../../../util'
|
||||
import {Config} from '../../../service/Config'
|
||||
@ -43,7 +43,7 @@ export class ClientRepositoryFactory extends AbstractFactory<ClientRepository> {
|
||||
let currentToken = this.getClientRepositoryClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
|
||||
Instantiable, FactoryProducer, getPropertyInjectionMetadata,
|
||||
} from '../../../di'
|
||||
import {Collection, ErrorWithContext} from '../../../util'
|
||||
import {Config} from '../../../service/Config'
|
||||
@ -43,7 +43,7 @@ export class RedemptionCodeRepositoryFactory extends AbstractFactory<RedemptionC
|
||||
let currentToken = this.getRedemptionCodeRepositoryClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
|
||||
Instantiable, FactoryProducer, getPropertyInjectionMetadata,
|
||||
} from '../../../di'
|
||||
import {Collection, ErrorWithContext} from '../../../util'
|
||||
import {Config} from '../../../service/Config'
|
||||
@ -43,7 +43,7 @@ export class ScopeRepositoryFactory extends AbstractFactory<ScopeRepository> {
|
||||
let currentToken = this.getScopeRepositoryClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, FactoryProducer,
|
||||
Instantiable, FactoryProducer, getPropertyInjectionMetadata,
|
||||
} from '../../../di'
|
||||
import {Collection, ErrorWithContext} from '../../../util'
|
||||
import {Config} from '../../../service/Config'
|
||||
@ -43,7 +43,7 @@ export class TokenRepositoryFactory extends AbstractFactory<TokenRepository> {
|
||||
let currentToken = this.getTokenRepositoryClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
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'
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable,
|
||||
Instantiable, getPropertyInjectionMetadata,
|
||||
} from '../../di'
|
||||
import {Collection, ErrorWithContext} from '../../util'
|
||||
import {MemorySession} from './MemorySession'
|
||||
@ -52,7 +52,7 @@ export class SessionFactory extends AbstractFactory<Session> {
|
||||
let currentToken = this.getSessionClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
@ -4,7 +4,11 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, Instantiable, Injectable, Inject, FactoryProducer,
|
||||
Instantiable,
|
||||
Injectable,
|
||||
Inject,
|
||||
FactoryProducer,
|
||||
getPropertyInjectionMetadata,
|
||||
} from '../../di'
|
||||
import {Collection, ErrorWithContext} from '../../util'
|
||||
import {Logging} from '../../service/Logging'
|
||||
@ -51,7 +55,7 @@ export class MigratorFactory extends AbstractFactory<Migrator> {
|
||||
let currentToken = this.getMigratorClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
@ -5,9 +5,8 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY,
|
||||
StaticInstantiable,
|
||||
FactoryProducer,
|
||||
FactoryProducer, getPropertyInjectionMetadata,
|
||||
} from '../../../di'
|
||||
import {Collection, ErrorWithContext} from '../../../util'
|
||||
import {Logging} from '../../../service/Logging'
|
||||
@ -52,7 +51,7 @@ export class QueueFactory extends AbstractFactory<Queue> {
|
||||
let currentToken = this.getQueueClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
4
src/support/cache/CacheFactory.ts
vendored
4
src/support/cache/CacheFactory.ts
vendored
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, StaticClass, Instantiable,
|
||||
StaticClass, Instantiable, getPropertyInjectionMetadata,
|
||||
} from '../../di'
|
||||
import {Collection, ErrorWithContext} from '../../util'
|
||||
import {Logging} from '../../service/Logging'
|
||||
@ -52,7 +52,7 @@ export class CacheFactory extends AbstractFactory<Cache> {
|
||||
let currentToken = this.getCacheClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import {
|
||||
PropertyDependency,
|
||||
isInstantiable,
|
||||
DEPENDENCY_KEYS_METADATA_KEY,
|
||||
DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, StaticClass, Instantiable,
|
||||
StaticClass, Instantiable, getPropertyInjectionMetadata,
|
||||
} from '../di'
|
||||
import {Collection, ErrorWithContext} from '../util'
|
||||
import {Logging} from '../service/Logging'
|
||||
@ -49,7 +49,7 @@ export class ViewEngineFactory extends AbstractFactory<ViewEngine> {
|
||||
let currentToken = this.getViewEngineClass()
|
||||
|
||||
do {
|
||||
const loadedMeta = Reflect.getMetadata(DEPENDENCY_KEYS_PROPERTY_METADATA_KEY, currentToken)
|
||||
const loadedMeta = getPropertyInjectionMetadata(currentToken)
|
||||
if ( loadedMeta ) {
|
||||
meta.concat(loadedMeta)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user