X Tutup
Skip to content

Commit de77700

Browse files
tboschIgorMinar
authored andcommitted
fix(di): throw if a token uses more than 20 dependencies.
Fixes #6690 Closes #6869
1 parent e73fee7 commit de77700

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

modules/angular2/src/core/di/injector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
OutOfBoundsError
1818
} from './exceptions';
1919
import {FunctionWrapper, Type, isPresent, isBlank, CONST_EXPR} from 'angular2/src/facade/lang';
20+
import {BaseException} from 'angular2/src/facade/exceptions';
2021
import {Key} from './key';
2122
import {SelfMetadata, HostMetadata, SkipSelfMetadata} from './metadata';
2223

@@ -874,6 +875,9 @@ export class Injector {
874875
obj = factory(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, d16,
875876
d17, d18, d19);
876877
break;
878+
default:
879+
throw new BaseException(
880+
`Cannot instantiate '${provider.key.displayName}' because it has more than 20 dependencies`);
877881
}
878882
} catch (e) {
879883
throw new InstantiationError(this, e, e.stack, provider.key);

modules/angular2/test/core/di/injector_spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,49 @@ export function main() {
189189
expect(car.engine).toBeAnInstanceOf(Engine);
190190
});
191191

192+
it('should throw when using a factory with more than 20 dependencies', () => {
193+
function factoryWithTooManyArgs() { return new Car(null); }
194+
195+
var injector = createInjector([
196+
Engine,
197+
provide(Car,
198+
{
199+
useFactory: factoryWithTooManyArgs,
200+
deps: [
201+
Engine,
202+
Engine,
203+
Engine,
204+
Engine,
205+
Engine,
206+
Engine,
207+
Engine,
208+
Engine,
209+
Engine,
210+
Engine,
211+
Engine,
212+
Engine,
213+
Engine,
214+
Engine,
215+
Engine,
216+
Engine,
217+
Engine,
218+
Engine,
219+
Engine,
220+
Engine,
221+
Engine
222+
]
223+
})
224+
]);
225+
226+
try {
227+
injector.get(Car);
228+
throw "Must throw";
229+
} catch (e) {
230+
expect(e.message)
231+
.toContain(`Cannot instantiate 'Car' because it has more than 20 dependencies`);
232+
}
233+
});
234+
192235
it('should supporting provider to null', () => {
193236
var injector = createInjector([provide(Engine, {useValue: null})]);
194237
var engine = injector.get(Engine);

0 commit comments

Comments
 (0)
X Tutup