Skip to content

Commit

Permalink
Fix circular dependency error message (#1627)
Browse files Browse the repository at this point in the history
* fix: updated planner to provide right circular dependent services

* docs: update changelog

* test: update test to avoid accessing class before initialization
  • Loading branch information
notaphplover authored Nov 13, 2024
1 parent 22cdcc0 commit d6f09a6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated planner with better error description when a binding can not be properly resolved.

### Fixed
- Updated planner to provide right circular dependent services when such dependencies are detected.

## [6.1.3]

Expand Down
2 changes: 1 addition & 1 deletion src/utils/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function dependencyChainToString(request: interfaces.Request) {

function circularDependencyToException(request: interfaces.Request) {
request.childRequests.forEach((childRequest: interfaces.Request) => {
if (alreadyDependencyChain(childRequest, childRequest.serviceIdentifier)) {
if (alreadyDependencyChain(request, childRequest.serviceIdentifier)) {
const services: string = dependencyChainToString(childRequest);
throw new Error(`${ERROR_MSGS.CIRCULAR_DEPENDENCY} ${services}`);
} else {
Expand Down
52 changes: 52 additions & 0 deletions test/bugs/issue_1515.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai';

import {
Container,
inject,
injectable,
multiInject,
} from '../../src/inversify';

describe('Issue 1515', () => {
it('should not throw on false circular dependency', () => {
@injectable()
class Circle1 {
constructor(@inject('circle-2') public readonly circle2: unknown) {}
}

@injectable()
class Circle2 {
constructor(@inject('circle-1') public circle1: unknown) {}
}

@injectable()
class Multi1 {}
@injectable()
class Multi2 {}
@injectable()
class Multi3 {}

@injectable()
class Top {
constructor(
@multiInject('multi-inject') public readonly multis: unknown[],
@inject('circle-1') public readonly circle1: unknown,
) {}
}

const container: Container = new Container();

container.bind('multi-inject').to(Multi1);
container.bind('multi-inject').to(Multi2);
container.bind('multi-inject').to(Multi3);
container.bind('circle-1').to(Circle1);
container.bind('circle-2').to(Circle2);
container.bind(Top).toSelf();

expect(() => {
container.get(Top);
}).to.throw(
'Circular dependency found: Top --> circle-1 --> circle-2 --> circle-1',
);
});
});

0 comments on commit d6f09a6

Please sign in to comment.