fix(core): don't override ngInjectableDef in the decorator if present on the type (#22943)

Previously, @Injectable() would generate an ngInjectableDef on the type it was
decorating, even if that type already had a compiled ngInjectableDef, overwriting
the compiled version.

PR Close #22943
This commit is contained in:
Alex Rickabaugh 2018-03-22 15:47:06 -07:00 committed by Matias Niemelä
parent 4f0cae0676
commit 6f0191744c
2 changed files with 20 additions and 1 deletions

View File

@ -124,4 +124,22 @@ describe('ngInjectableDef Bazel Integration', () => {
expect(TestBed.get(Service).value).toEqual('overridden');
});
it('does not override existing ngInjectableDef', () => {
@Injectable({
providedIn: 'root',
useValue: new Service(false),
})
class Service {
constructor(public value: boolean) {}
static ngInjectableDef = {
providedIn: 'root',
factory: () => new Service(true),
token: Service,
};
}
TestBed.configureTestingModule({});
expect(TestBed.get(Service).value).toEqual(true);
});
});

View File

@ -119,7 +119,8 @@ export const Injectable: InjectableDecorator = makeDecorator(
'Injectable', undefined, undefined, undefined,
(injectableType: InjectableType<any>,
options: {providedIn?: Type<any>| 'root' | null} & InjectableProvider) => {
if (options && options.providedIn !== undefined) {
if (options && options.providedIn !== undefined &&
injectableType.ngInjectableDef === undefined) {
injectableType.ngInjectableDef = defineInjectable({
providedIn: options.providedIn,
factory: convertInjectableProviderToFactory(injectableType, options)