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: prevent trying to inject arrays #674

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/container-instance.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export class ContainerInstance {
* Checks if given parameter type is primitive type or not.
*/
private isPrimitiveParamType(paramTypeName: string): boolean {
return ['string', 'boolean', 'number', 'object'].includes(paramTypeName.toLowerCase());
return ['string', 'boolean', 'number', 'object', 'array'].includes(paramTypeName.toLowerCase());
}

/**
Expand Down
39 changes: 39 additions & 0 deletions test/github-issues/530/issue-530.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'reflect-metadata';
import { Container } from '../../../src/index';
import { Service } from '../../../src/decorators/service.decorator';
import { Inject } from '../../../src/decorators/inject.decorator';
import { Token } from '../../../src/token.class';

describe('Github Issues', function () {
beforeEach(() => Container.reset({ strategy: 'resetValue' }));

it('#530 - inject should ignore array types', () => {
const token = new Token('token');
Container.set({ id: token, value: 'tokenValue' });

@Service()
class TestClass {}

const classInstance = new TestClass();
Container.set({ id: TestClass, value: classInstance });

@Service()
class Test {
public injectedValue: string;
public injectedClass: TestClass;
public notInjectedArray;

constructor(@Inject(token) injected: string, injectedClass: TestClass, notInjected?: string[]) {
this.injectedValue = injected;
this.injectedClass = injectedClass;
this.notInjectedArray = notInjected;
}
}

const resolvedClass = Container.get<Test>(Test);

expect(resolvedClass.injectedClass).toBe(classInstance);
expect(resolvedClass.notInjectedArray).toBe(undefined);
expect(resolvedClass.injectedValue).toBe('tokenValue');
});
});