Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/injector/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,12 +835,14 @@ export class Injector {
: new (metatype as Type<any>)(...instances);

instanceHost.instance = this.instanceDecorator(instanceHost.instance);
instanceHost.isConstructorCalled = true;
} else if (isInContext) {
const factoryReturnValue = (targetMetatype.metatype as any as Function)(
...instances,
);
instanceHost.instance = await factoryReturnValue;
instanceHost.instance = this.instanceDecorator(instanceHost.instance);
instanceHost.isConstructorCalled = true;
}
instanceHost.isResolved = true;
return instanceHost.instance;
Expand Down
7 changes: 6 additions & 1 deletion packages/core/injector/instance-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface InstancePerContext<T> {
isResolved?: boolean;
isPending?: boolean;
donePromise?: Promise<unknown>;
isConstructorCalled?: boolean;
}

export interface PropertyMetadata {
Expand Down Expand Up @@ -439,7 +440,11 @@ export class InstanceWrapper<T = any> {
const instances = [...this.transientMap.values()];
return iterate(instances)
.map(item => item.get(STATIC_CONTEXT))
.filter(item => !!item)
.filter(item => {
// Only return items where constructor has been actually called
// This prevents calling lifecycle hooks on non-instantiated transient services
return !!(item && item.isConstructorCalled);
})
.toArray();
}

Expand Down
50 changes: 49 additions & 1 deletion packages/core/test/injector/instance-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,16 +896,64 @@ describe('InstanceWrapper', () => {
});
});
describe('when instance is transient', () => {
it('should return all static instances', () => {
it('should return instances where constructor was called', () => {
const wrapper = new InstanceWrapper({
scope: Scope.TRANSIENT,
});
const instanceHost = {
instance: {},
isConstructorCalled: true,
};
wrapper.setInstanceByInquirerId(STATIC_CONTEXT, 'test', instanceHost);
expect(wrapper.getStaticTransientInstances()).to.be.eql([instanceHost]);
});

describe('lifecycle hooks on transient services', () => {
// Tests for issue #15553: prevent lifecycle hooks on non-instantiated transient services
it('should filter out instances created with Object.create (prototype only)', () => {
const wrapper = new InstanceWrapper({
scope: Scope.TRANSIENT,
});
// Simulates what happens in cloneTransientInstance
const prototypeOnlyInstance = {
instance: Object.create({}),
isResolved: true, // This is set to true incorrectly in injector.ts
isConstructorCalled: false, // But constructor was never called
};
wrapper.setInstanceByInquirerId(
STATIC_CONTEXT,
'inquirer',
prototypeOnlyInstance,
);

// Should not include this instance for lifecycle hooks
expect(wrapper.getStaticTransientInstances()).to.be.eql([]);
});

it('should include instances where constructor was actually invoked', () => {
class TestService {}
const wrapper = new InstanceWrapper({
scope: Scope.TRANSIENT,
metatype: TestService,
});
// Simulates what happens after instantiateClass
const properInstance = {
instance: new TestService(),
isResolved: true,
isConstructorCalled: true,
};
wrapper.setInstanceByInquirerId(
STATIC_CONTEXT,
'inquirer',
properInstance,
);

// Should include this instance for lifecycle hooks
expect(wrapper.getStaticTransientInstances()).to.be.eql([
properInstance,
]);
});
});
});
});

Expand Down
Loading