Skip to content

Commit

Permalink
Merge pull request #9456 from MyAeroCode/lutz-fix/scoped-injection-wi…
Browse files Browse the repository at this point in the history
…th-symbol

fix(core): scoped injection with symbol field name
  • Loading branch information
kamilmysliwiec authored May 13, 2022
2 parents d14a944 + 3601f1a commit 24e67a5
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 12 deletions.
23 changes: 13 additions & 10 deletions packages/core/injector/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
isNil,
isObject,
isString,
isSymbol,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import { iterate } from 'iterare';
Expand All @@ -45,7 +46,7 @@ export type InjectorDependency = InjectionToken;
* The property-based dependency
*/
export interface PropertyDependency {
key: string;
key: symbol | string;
name: InjectorDependency;
isOptional?: boolean;
instance?: any;
Expand Down Expand Up @@ -360,7 +361,7 @@ export class Injector {
moduleRef: Module,
contextId = STATIC_CONTEXT,
inquirer?: InstanceWrapper,
keyOrIndex?: string | number,
keyOrIndex?: symbol | string | number,
) {
if (isUndefined(param)) {
this.logger.log(
Expand Down Expand Up @@ -402,7 +403,7 @@ export class Injector {
wrapper: InstanceWrapper<T>,
contextId = STATIC_CONTEXT,
inquirer?: InstanceWrapper,
keyOrIndex?: string | number,
keyOrIndex?: symbol | string | number,
): Promise<InstanceWrapper> {
this.printResolvingDependenciesLog(token, inquirer);
this.printLookingForProviderLog(token, moduleRef);
Expand Down Expand Up @@ -478,7 +479,7 @@ export class Injector {
wrapper: InstanceWrapper<T>,
contextId = STATIC_CONTEXT,
inquirer?: InstanceWrapper,
keyOrIndex?: string | number,
keyOrIndex?: symbol | string | number,
): Promise<InstanceWrapper<T>> {
const token = wrapper.token || wrapper.name;
const { name } = dependencyContext;
Expand Down Expand Up @@ -511,7 +512,7 @@ export class Injector {
wrapper: InstanceWrapper<T>,
contextId = STATIC_CONTEXT,
inquirer?: InstanceWrapper,
keyOrIndex?: string | number,
keyOrIndex?: symbol | string | number,
) {
const instanceWrapper = await this.lookupComponentInImports(
moduleRef,
Expand Down Expand Up @@ -539,7 +540,7 @@ export class Injector {
moduleRegistry: any[] = [],
contextId = STATIC_CONTEXT,
inquirer?: InstanceWrapper,
keyOrIndex?: string | number,
keyOrIndex?: symbol | string | number,
isTraversing?: boolean,
): Promise<any> {
let instanceWrapperRef: InstanceWrapper = null;
Expand Down Expand Up @@ -830,13 +831,15 @@ export class Injector {
}

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

private getTokenName(token: InstanceToken): string {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/injector/instance-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface InstancePerContext<T> {
donePromise?: Promise<void>;
}
export interface PropertyMetadata {
key: string;
key: symbol | string;
wrapper: InstanceWrapper;
}

Expand Down Expand Up @@ -146,7 +146,7 @@ export class InstanceWrapper<T = any> {
return this[INSTANCE_METADATA_SYMBOL].dependencies;
}

public addPropertiesMetadata(key: string, wrapper: InstanceWrapper) {
public addPropertiesMetadata(key: symbol | string, wrapper: InstanceWrapper) {
if (!this[INSTANCE_METADATA_SYMBOL].properties) {
this[INSTANCE_METADATA_SYMBOL].properties = [];
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/nest-application-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ export class NestApplicationContext implements INestApplicationContext {
wrapperRef.host,
collection,
contextId,
wrapperRef,
);
if (!instance) {
throw new UnknownElementException();
Expand Down
53 changes: 53 additions & 0 deletions packages/core/test/injector/injector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,4 +852,57 @@ describe('Injector', () => {
expect(optionalDependenciesIds).to.deep.eq([1]);
});
});

describe('addDependencyMetadata', () => {
interface IInjector extends Omit<Injector, 'addDependencyMetadata'> {
addDependencyMetadata: (
keyOrIndex: symbol | string | number,
hostWrapper: InstanceWrapper,
instanceWrapper: InstanceWrapper,
) => void;
}

let exposedInjector: IInjector;
let hostWrapper: InstanceWrapper;
let instanceWrapper: InstanceWrapper;

beforeEach(() => {
exposedInjector = injector as unknown as IInjector;
hostWrapper = new InstanceWrapper();
instanceWrapper = new InstanceWrapper();
});

it('should add dependency metadata to PropertiesMetadata when key is symbol', async () => {
const addPropertiesMetadataSpy = sinon.spy(
hostWrapper,
'addPropertiesMetadata',
);

const key = Symbol.for('symbol');
exposedInjector.addDependencyMetadata(key, hostWrapper, instanceWrapper);

expect(addPropertiesMetadataSpy.called).to.be.true;
});

it('should add dependency metadata to PropertiesMetadata when key is string', async () => {
const addPropertiesMetadataSpy = sinon.spy(
hostWrapper,
'addPropertiesMetadata',
);

const key = 'string';
exposedInjector.addDependencyMetadata(key, hostWrapper, instanceWrapper);

expect(addPropertiesMetadataSpy.called).to.be.true;
});

it('should add dependency metadata to CtorMetadata when key is number', async () => {
const addCtorMetadataSpy = sinon.spy(hostWrapper, 'addCtorMetadata');

const key = 0;
exposedInjector.addDependencyMetadata(key, hostWrapper, instanceWrapper);

expect(addCtorMetadataSpy.called).to.be.true;
});
});
});
Loading

0 comments on commit 24e67a5

Please sign in to comment.