fix(ivy): provided Injector should be instantiated by its factory (#27456)
(FW-777) When an Injector is provided, R3Injector instantiates it by calling its constructor instead of its factory, not resolving dependencies. With this fix, the ngInjectorDef is checked and the factory is correctly used if it is found. PR Close #27456
This commit is contained in:
@ -166,7 +166,7 @@ export class R3Injector {
|
||||
if (def && this.injectableDefInScope(def)) {
|
||||
// Found an ngInjectableDef and it's scoped to this injector. Pretend as if it was here
|
||||
// all along.
|
||||
record = makeRecord(injectableDefFactory(token), NOT_YET);
|
||||
record = makeRecord(injectableDefOrInjectorDefFactory(token), NOT_YET);
|
||||
this.records.set(token, record);
|
||||
}
|
||||
}
|
||||
@ -339,9 +339,14 @@ export class R3Injector {
|
||||
}
|
||||
}
|
||||
|
||||
function injectableDefFactory(token: Type<any>| InjectionToken<any>): () => any {
|
||||
function injectableDefOrInjectorDefFactory(token: Type<any>| InjectionToken<any>): () => any {
|
||||
const injectableDef = getInjectableDef(token as InjectableType<any>);
|
||||
if (injectableDef === null) {
|
||||
const injectorDef = getInjectorDef(token as InjectorType<any>);
|
||||
if (injectorDef !== null) {
|
||||
return injectorDef.factory;
|
||||
}
|
||||
|
||||
if (token instanceof InjectionToken) {
|
||||
throw new Error(`Token ${stringify(token)} is missing an ngInjectableDef definition.`);
|
||||
}
|
||||
@ -369,7 +374,7 @@ function providerToRecord(provider: SingleProvider): Record<any> {
|
||||
export function providerToFactory(provider: SingleProvider): () => any {
|
||||
let factory: (() => any)|undefined = undefined;
|
||||
if (isTypeProvider(provider)) {
|
||||
return injectableDefFactory(resolveForwardRef(provider));
|
||||
return injectableDefOrInjectorDefFactory(resolveForwardRef(provider));
|
||||
} else {
|
||||
if (isValueProvider(provider)) {
|
||||
factory = () => resolveForwardRef(provider.useValue);
|
||||
@ -383,7 +388,7 @@ export function providerToFactory(provider: SingleProvider): () => any {
|
||||
if (hasDeps(provider)) {
|
||||
factory = () => new (classRef)(...injectArgs(provider.deps));
|
||||
} else {
|
||||
return injectableDefFactory(classRef);
|
||||
return injectableDefOrInjectorDefFactory(classRef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1008,7 +1008,7 @@
|
||||
"name": "injectRootLimpMode"
|
||||
},
|
||||
{
|
||||
"name": "injectableDefFactory"
|
||||
"name": "injectableDefOrInjectorDefFactory"
|
||||
},
|
||||
{
|
||||
"name": "insertBloom"
|
||||
|
@ -141,7 +141,7 @@
|
||||
"name": "injectRootLimpMode"
|
||||
},
|
||||
{
|
||||
"name": "injectableDefFactory"
|
||||
"name": "injectableDefOrInjectorDefFactory"
|
||||
},
|
||||
{
|
||||
"name": "isExistingProvider"
|
||||
|
@ -2082,7 +2082,7 @@
|
||||
"name": "injectViewContainerRef"
|
||||
},
|
||||
{
|
||||
"name": "injectableDefFactory"
|
||||
"name": "injectableDefOrInjectorDefFactory"
|
||||
},
|
||||
{
|
||||
"name": "insertBloom"
|
||||
|
@ -121,6 +121,14 @@ describe('InjectorDef-based createInjector()', () => {
|
||||
});
|
||||
}
|
||||
|
||||
class InjectorWithDep {
|
||||
constructor(readonly service: Service) {}
|
||||
|
||||
static ngInjectorDef = defineInjector({
|
||||
factory: () => new InjectorWithDep(inject(Service)),
|
||||
});
|
||||
}
|
||||
|
||||
class Module {
|
||||
static ngInjectorDef = defineInjector({
|
||||
factory: () => new Module(),
|
||||
@ -137,6 +145,7 @@ describe('InjectorDef-based createInjector()', () => {
|
||||
CircularA,
|
||||
CircularB,
|
||||
{provide: STATIC_TOKEN, useClass: StaticService, deps: [Service]},
|
||||
InjectorWithDep,
|
||||
],
|
||||
});
|
||||
}
|
||||
@ -204,6 +213,12 @@ describe('InjectorDef-based createInjector()', () => {
|
||||
expect(instance.locale).toEqual(['en', 'es']);
|
||||
});
|
||||
|
||||
it('injects an injector with dependencies', () => {
|
||||
const instance = injector.get(InjectorWithDep);
|
||||
expect(instance instanceof InjectorWithDep);
|
||||
expect(instance.service).toBe(injector.get(Service));
|
||||
});
|
||||
|
||||
it('injects a token with useExisting', () => {
|
||||
const instance = injector.get(SERVICE_TOKEN);
|
||||
expect(instance).toBe(injector.get(Service));
|
||||
|
Reference in New Issue
Block a user