Skip to content

Commit b1506a3

Browse files
committed
fix(ivy): support injection flags for provider deps without new (angular#30216)
Previously, we were supporting injection flags for provider deps, but only if they fit the format `new Optional()`. This commit fixes resolution of provider deps to also support `Optional` (without the new). This keeps us backwards compatible with what View Engine supported. PR Close angular#30216
1 parent b15a403 commit b1506a3

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

packages/core/src/di/injector_compatibility.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,14 @@ export function injectArgs(types: (Type<any>| InjectionToken<any>| any[])[]): an
145145

146146
for (let j = 0; j < arg.length; j++) {
147147
const meta = arg[j];
148-
if (meta instanceof Optional || meta.ngMetadataName === 'Optional') {
148+
if (meta instanceof Optional || meta.ngMetadataName === 'Optional' || meta === Optional) {
149149
flags |= InjectFlags.Optional;
150-
} else if (meta instanceof SkipSelf || meta.ngMetadataName === 'SkipSelf') {
150+
} else if (
151+
meta instanceof SkipSelf || meta.ngMetadataName === 'SkipSelf' || meta === SkipSelf) {
151152
flags |= InjectFlags.SkipSelf;
152-
} else if (meta instanceof Self || meta.ngMetadataName === 'Self') {
153+
} else if (meta instanceof Self || meta.ngMetadataName === 'Self' || meta === Self) {
153154
flags |= InjectFlags.Self;
154-
} else if (meta instanceof Inject) {
155+
} else if (meta instanceof Inject || meta === Inject) {
155156
type = meta.token;
156157
} else {
157158
type = meta;

packages/core/test/acceptance/providers_spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Component, Directive, Inject, Injectable, InjectionToken, NgModule, forwardRef} from '@angular/core';
9+
import {Component, Directive, Inject, Injectable, InjectionToken, Injector, NgModule, Optional, forwardRef} from '@angular/core';
1010
import {TestBed, async, inject} from '@angular/core/testing';
1111
import {By} from '@angular/platform-browser';
1212
import {onlyInIvy} from '@angular/private/testing';
@@ -321,4 +321,25 @@ describe('providers', () => {
321321

322322
});
323323

324+
describe('flags', () => {
325+
326+
class MyService {
327+
constructor(public value: OtherService|null) {}
328+
}
329+
330+
class OtherService {}
331+
332+
it('should support Optional flag in deps', () => {
333+
const injector =
334+
Injector.create([{provide: MyService, deps: [[new Optional(), OtherService]]}]);
335+
336+
expect(injector.get(MyService).value).toBe(null);
337+
});
338+
339+
it('should support Optional flag in deps without instantiating it', () => {
340+
const injector = Injector.create([{provide: MyService, deps: [[Optional, OtherService]]}]);
341+
342+
expect(injector.get(MyService).value).toBe(null);
343+
});
344+
});
324345
});

0 commit comments

Comments
 (0)