Skip to content

Commit 7abecdb

Browse files
#98 Wrong injections order when using Inject decorator
1 parent 997436a commit 7abecdb

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

src/lib/decorators.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ export function Injectable(injections?: ProviderToken[]) {
1010
MetadataAnnotator.defineMetadata(INJECTABLE_MD_KEY, true, target);
1111

1212
if (injections && Array.isArray(injections)) {
13-
const injectionMd: IInjectionMd[] = MetadataAnnotator.getMetadata(INJECTIONS_MD_KEY, target) || [];
14-
injections.forEach(token => injectionMd.push(token));
15-
MetadataAnnotator.defineMetadata(INJECTIONS_MD_KEY, injectionMd, target);
13+
const injectionsMd: IInjectionMd[] = MetadataAnnotator.getMetadata(INJECTIONS_MD_KEY, target) || [];
14+
injections.forEach(token => injectionsMd.push(token));
15+
MetadataAnnotator.defineMetadata(INJECTIONS_MD_KEY, injectionsMd, target);
1616
}
1717
};
1818
}
1919

2020
export function Inject(token: any) {
2121
return (target: object, propertyKey: string | symbol, parameterIndex: number) => {
2222
const injections: IInjectionMd[] = MetadataAnnotator.getMetadata(INJECTIONS_MD_KEY, target) || [];
23-
injections.push(token);
23+
injections[parameterIndex] = token;
2424
MetadataAnnotator.defineMetadata(INJECTIONS_MD_KEY, injections, target);
2525
};
2626
}

src/tests/decorators.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ describe('Decorators', () => {
131131

132132
expect(A.works).to.be.true;
133133
});
134+
135+
it('should inject parameters in the right order', () => {
136+
@Injectable()
137+
class A {}
138+
139+
@Injectable()
140+
class B {}
141+
142+
@Injectable()
143+
class C {
144+
constructor(
145+
@Inject(A) public a: A,
146+
@Inject(B) public b: B
147+
) {
148+
149+
}
150+
}
151+
152+
const providers = [
153+
{ token: A, useClass: A },
154+
{ token: B, useClass: B },
155+
{ token: C, useClass: C }
156+
];
157+
158+
container.register(providers);
159+
160+
const c = container.resolve(C);
161+
162+
expect(c.a).to.be.instanceof(A);
163+
expect(c.b).to.be.instanceof(B);
164+
});
134165
});
135166

136167
describe('@Injectable()', () => {
@@ -152,5 +183,36 @@ describe('Decorators', () => {
152183
container.register(A);
153184
expect(() => container.resolve(A)).to.throw();
154185
});
186+
187+
it('should inject parameters in the right order when using alternative way of declaring dependencies', () => {
188+
@Injectable()
189+
class A {}
190+
191+
@Injectable()
192+
class B {}
193+
194+
@Injectable([A, B])
195+
class C {
196+
constructor(
197+
public a: A,
198+
public b: B
199+
) {
200+
201+
}
202+
}
203+
204+
const providers = [
205+
{ token: A, useClass: A },
206+
{ token: B, useClass: B },
207+
{ token: C, useClass: C }
208+
];
209+
210+
container.register(providers);
211+
212+
const c = container.resolve(C);
213+
214+
expect(c.a).to.be.instanceof(A);
215+
expect(c.b).to.be.instanceof(B);
216+
});
155217
});
156218
});

0 commit comments

Comments
 (0)