diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a376dcb..40484f63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/src/utils/serialization.ts b/src/utils/serialization.ts index eac24bc5..5acde4b8 100644 --- a/src/utils/serialization.ts +++ b/src/utils/serialization.ts @@ -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 { diff --git a/test/bugs/issue_1515.test.ts b/test/bugs/issue_1515.test.ts new file mode 100644 index 00000000..4068d9cc --- /dev/null +++ b/test/bugs/issue_1515.test.ts @@ -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', + ); + }); +});