fix(ivy): optional dependencies should not throw with module injector (#26763)

PR Close #26763
This commit is contained in:
Kara Erickson 2018-10-25 11:35:51 -07:00 committed by Matias Niemelä
parent 1130e48541
commit 96770e5c6b
4 changed files with 94 additions and 44 deletions

View File

@ -178,8 +178,8 @@ export class R3Injector {
// Select the next injector based on the Self flag - if self is set, the next injector is // Select the next injector based on the Self flag - if self is set, the next injector is
// the NullInjector, otherwise it's the parent. // the NullInjector, otherwise it's the parent.
let next = !(flags & InjectFlags.Self) ? this.parent : getNullInjector(); const nextInjector = !(flags & InjectFlags.Self) ? this.parent : getNullInjector();
return this.parent.get(token, notFoundValue); return nextInjector.get(token, notFoundValue);
} finally { } finally {
// Lastly, clean up the state by restoring the previous injector. // Lastly, clean up the state by restoring the previous injector.
setCurrentInjector(previousInjector); setCurrentInjector(previousInjector);

View File

@ -368,6 +368,11 @@ export function getOrCreateInjectable<T>(
} }
} }
if (flags & InjectFlags.Optional && notFoundValue === undefined) {
// This must be set or the NullInjector will throw for optional deps
notFoundValue = null;
}
if ((flags & (InjectFlags.Self | InjectFlags.Host)) === 0) { if ((flags & (InjectFlags.Self | InjectFlags.Host)) === 0) {
const moduleInjector = lViewData[INJECTOR]; const moduleInjector = lViewData[INJECTOR];
if (moduleInjector) { if (moduleInjector) {

View File

@ -1565,10 +1565,8 @@ describe('providers', () => {
parent: { parent: {
componentAssertion: () => { componentAssertion: () => {
expect(directiveInject(String)).toEqual('Module'); expect(directiveInject(String)).toEqual('Module');
expect(directiveInject(String, InjectFlags.Optional | InjectFlags.Self)) expect(directiveInject(String, InjectFlags.Optional | InjectFlags.Self)).toBeNull();
.toEqual(undefined as any); expect(directiveInject(String, InjectFlags.Optional | InjectFlags.Host)).toBeNull();
expect(directiveInject(String, InjectFlags.Optional | InjectFlags.Host))
.toEqual(undefined as any);
} }
} }
}); });

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {Attribute, ChangeDetectorRef, ElementRef, Host, InjectFlags, Injector, Optional, Renderer2, Self, SkipSelf, TemplateRef, ViewContainerRef, defineInjectable} from '@angular/core'; import {Attribute, ChangeDetectorRef, ElementRef, Host, InjectFlags, Injector, Optional, Renderer2, Self, SkipSelf, TemplateRef, ViewContainerRef, createInjector, defineInjectable, defineInjector} from '@angular/core';
import {RenderFlags} from '@angular/core/src/render3/interfaces/definition'; import {RenderFlags} from '@angular/core/src/render3/interfaces/definition';
import {defineComponent} from '../../src/render3/definition'; import {defineComponent} from '../../src/render3/definition';
@ -857,8 +857,8 @@ describe('di', () => {
}); });
} }
it('should not throw if dependency is @Optional', () => { describe('Optional', () => {
let dirA: DirA; let dirA: DirA|null = null;
class DirA { class DirA {
constructor(@Optional() public dirB: DirB|null) {} constructor(@Optional() public dirB: DirB|null) {}
@ -870,47 +870,94 @@ describe('di', () => {
}); });
} }
/** <div dirA></div> */ beforeEach(() => dirA = null);
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
if (rf & RenderFlags.Create) {
element(0, 'div', ['dirA', '']);
}
}, 1, 0, [DirA, DirB]);
expect(() => { it('should not throw if dependency is @Optional (limp mode)', () => {
new ComponentFixture(App);
/** <div dirA></div> */
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
if (rf & RenderFlags.Create) {
element(0, 'div', ['dirA', '']);
}
}, 1, 0, [DirA, DirB]);
expect(() => { new ComponentFixture(App); }).not.toThrow();
expect(dirA !.dirB).toEqual(null); expect(dirA !.dirB).toEqual(null);
}).not.toThrow(); });
});
it('should not throw if dependency is @Optional but defined elsewhere', () => { it('should not throw if dependency is @Optional (module injector)', () => {
let dirA: DirA; class SomeModule {
static ngInjectorDef = defineInjector({factory: () => new SomeModule()});
class DirA {
constructor(@Optional() public dirB: DirB|null) {}
static ngDirectiveDef = defineDirective({
type: DirA,
selectors: [['', 'dirA', '']],
factory: () => dirA = new DirA(directiveInject(DirB, InjectFlags.Optional))
});
}
/**
* <div dirB></div>
* <div dirA></div>
*/
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
if (rf & RenderFlags.Create) {
element(0, 'div', ['dirB', '']);
element(1, 'div', ['dirA', '']);
} }
}, 2, 0, [DirA, DirB]);
expect(() => { /** <div dirA></div> */
new ComponentFixture(App); const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
if (rf & RenderFlags.Create) {
element(0, 'div', ['dirA', '']);
}
}, 1, 0, [DirA, DirB]);
expect(() => {
const injector = createInjector(SomeModule);
new ComponentFixture(App, {injector});
}).not.toThrow();
expect(dirA !.dirB).toEqual(null); expect(dirA !.dirB).toEqual(null);
}).not.toThrow(); });
it('should return null if @Optional dependency has @Self flag', () => {
let dirC !: DirC;
class DirC {
constructor(@Optional() @Self() public dirB: DirB|null) {}
static ngDirectiveDef = defineDirective({
type: DirC,
selectors: [['', 'dirC', '']],
factory: () => dirC =
new DirC(directiveInject(DirB, InjectFlags.Optional|InjectFlags.Self))
});
}
/** <div dirC></div> */
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
if (rf & RenderFlags.Create) {
element(0, 'div', ['dirC', '']);
}
}, 1, 0, [DirC, DirB]);
expect(() => { new ComponentFixture(App); }).not.toThrow();
expect(dirC !.dirB).toEqual(null);
});
it('should not throw if dependency is @Optional but defined elsewhere', () => {
let dirA: DirA;
class DirA {
constructor(@Optional() public dirB: DirB|null) {}
static ngDirectiveDef = defineDirective({
type: DirA,
selectors: [['', 'dirA', '']],
factory: () => dirA = new DirA(directiveInject(DirB, InjectFlags.Optional))
});
}
/**
* <div dirB></div>
* <div dirA></div>
*/
const App = createComponent('app', function(rf: RenderFlags, ctx: any) {
if (rf & RenderFlags.Create) {
element(0, 'div', ['dirB', '']);
element(1, 'div', ['dirA', '']);
}
}, 2, 0, [DirA, DirB]);
expect(() => {
new ComponentFixture(App);
expect(dirA !.dirB).toEqual(null);
}).not.toThrow();
});
}); });
it('should skip the current node with @SkipSelf', () => { it('should skip the current node with @SkipSelf', () => {