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(di): throw if a token uses more than 20 dependencies. #6869

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
4 changes: 4 additions & 0 deletions modules/angular2/src/core/di/injector.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
Expand Down
43 changes: 43 additions & 0 deletions modules/angular2/test/core/di/injector_spec.ts
Expand Up @@ -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);
Expand Down