From de77700da0361ccd17a6b8db7ae6e6f940e70139 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Wed, 3 Feb 2016 09:51:24 -0800 Subject: [PATCH] fix(di): throw if a token uses more than 20 dependencies. Fixes #6690 Closes #6869 --- modules/angular2/src/core/di/injector.ts | 4 ++ .../angular2/test/core/di/injector_spec.ts | 43 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/modules/angular2/src/core/di/injector.ts b/modules/angular2/src/core/di/injector.ts index 4f846c9923..67d797b1a0 100644 --- a/modules/angular2/src/core/di/injector.ts +++ b/modules/angular2/src/core/di/injector.ts @@ -17,6 +17,7 @@ import { OutOfBoundsError } from './exceptions'; import {FunctionWrapper, Type, isPresent, isBlank, CONST_EXPR} from 'angular2/src/facade/lang'; +import {BaseException} from 'angular2/src/facade/exceptions'; import {Key} from './key'; import {SelfMetadata, HostMetadata, SkipSelfMetadata} from './metadata'; @@ -874,6 +875,9 @@ export class Injector { obj = factory(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16, d17, d18, d19); break; + default: + throw new BaseException( + `Cannot instantiate '${provider.key.displayName}' because it has more than 20 dependencies`); } } catch (e) { throw new InstantiationError(this, e, e.stack, provider.key); diff --git a/modules/angular2/test/core/di/injector_spec.ts b/modules/angular2/test/core/di/injector_spec.ts index e6f9c5d0b9..471e0e5f39 100644 --- a/modules/angular2/test/core/di/injector_spec.ts +++ b/modules/angular2/test/core/di/injector_spec.ts @@ -189,6 +189,49 @@ export function main() { expect(car.engine).toBeAnInstanceOf(Engine); }); + it('should throw when using a factory with more than 20 dependencies', () => { + function factoryWithTooManyArgs() { return new Car(null); } + + var injector = createInjector([ + Engine, + provide(Car, + { + useFactory: factoryWithTooManyArgs, + deps: [ + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine, + Engine + ] + }) + ]); + + try { + injector.get(Car); + throw "Must throw"; + } catch (e) { + expect(e.message) + .toContain(`Cannot instantiate 'Car' because it has more than 20 dependencies`); + } + }); + it('should supporting provider to null', () => { var injector = createInjector([provide(Engine, {useValue: null})]); var engine = injector.get(Engine);