From 6f0191744cde73a5d4ea359e1a00c020b1de42e2 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Thu, 22 Mar 2018 15:47:06 -0700 Subject: [PATCH] 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 --- .../bazel/injectable_def/app/test/app_spec.ts | 18 ++++++++++++++++++ packages/core/src/di/injectable.ts | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/integrationtest/bazel/injectable_def/app/test/app_spec.ts b/packages/compiler-cli/integrationtest/bazel/injectable_def/app/test/app_spec.ts index 452d437a9c..4db7761d7e 100644 --- a/packages/compiler-cli/integrationtest/bazel/injectable_def/app/test/app_spec.ts +++ b/packages/compiler-cli/integrationtest/bazel/injectable_def/app/test/app_spec.ts @@ -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); + }); }); diff --git a/packages/core/src/di/injectable.ts b/packages/core/src/di/injectable.ts index d58884699b..44f6431138 100644 --- a/packages/core/src/di/injectable.ts +++ b/packages/core/src/di/injectable.ts @@ -119,7 +119,8 @@ export const Injectable: InjectableDecorator = makeDecorator( 'Injectable', undefined, undefined, undefined, (injectableType: InjectableType, options: {providedIn?: Type| '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)