Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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);
});
});
3 changes: 2 additions & 1 deletion packages/core/src/di/injectable.ts
Expand Up @@ -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)
Expand Down