-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12020 from nestjs/fix/throw-circular-custom-provi…
…ders fix(core): throw on circular factories and custom providers
- Loading branch information
Showing
5 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
integration/injector/e2e/circular-custom-providers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
|
||
import { Controller, Injectable, Module } from '@nestjs/common'; | ||
|
||
class B {} | ||
|
||
@Injectable() | ||
class A { | ||
constructor(b: B) {} | ||
} | ||
|
||
@Injectable() | ||
class BImpl { | ||
constructor(a: A) {} | ||
} | ||
|
||
@Controller() | ||
class AppController { | ||
constructor(a: A) {} | ||
} | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [AppController], | ||
providers: [A, { provide: B, useClass: BImpl }], | ||
}) | ||
export class AppModule {} | ||
|
||
describe('Circular custom providers', () => { | ||
it('should throw an exception (useClass + regular provider)', async () => { | ||
try { | ||
const builder = Test.createTestingModule({ | ||
imports: [AppModule], | ||
}); | ||
await builder.compile(); | ||
|
||
expect(true).to.be.eql(false); | ||
} catch (err) { | ||
expect(err.message).to.be.eql( | ||
'A circular dependency has been detected inside "A". Please, make sure that each side of a bidirectional relationships are decorated with "forwardRef()". Note that circular relationships between custom providers (e.g., factories) are not supported since functions cannot be called more than once.', | ||
); | ||
} | ||
}); | ||
|
||
it('should throw an exception (2 factories)', async () => { | ||
try { | ||
const builder = Test.createTestingModule({ | ||
providers: [ | ||
{ provide: 'ABC', useFactory: () => ({}), inject: ['DEF'] }, | ||
{ provide: 'DEF', useFactory: () => ({}), inject: ['ABC'] }, | ||
], | ||
}); | ||
await builder.compile(); | ||
|
||
expect(true).to.be.eql(false); | ||
} catch (err) { | ||
expect(err.message).to.be.eql( | ||
'A circular dependency has been detected inside "ABC". Please, make sure that each side of a bidirectional relationships are decorated with "forwardRef()". Note that circular relationships between custom providers (e.g., factories) are not supported since functions cannot be called more than once.', | ||
); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* SettlementSignal is used to signal the resolution of a provider/instance. | ||
* Calling `complete` or `error` will resolve the promise returned by `asPromise`. | ||
* Can be used to detect circular dependencies. | ||
*/ | ||
export class SettlementSignal { | ||
private readonly _refs = new Set(); | ||
private readonly settledPromise: Promise<unknown>; | ||
private settleFn!: (err?: unknown) => void; | ||
|
||
constructor() { | ||
this.settledPromise = new Promise<unknown>(resolve => { | ||
this.settleFn = resolve; | ||
}); | ||
} | ||
|
||
/** | ||
* Resolves the promise returned by `asPromise`. | ||
*/ | ||
public complete() { | ||
this.settleFn(); | ||
} | ||
|
||
/** | ||
* Rejects the promise returned by `asPromise` with the given error. | ||
* @param err Error to reject the promise returned by `asPromise` with. | ||
*/ | ||
public error(err: unknown) { | ||
this.settleFn(err); | ||
} | ||
|
||
/** | ||
* Returns a promise that will be resolved when `complete` or `error` is called. | ||
* @returns Promise that will be resolved when `complete` or `error` is called. | ||
*/ | ||
public asPromise() { | ||
return this.settledPromise; | ||
} | ||
|
||
/** | ||
* Inserts a wrapper id that the host of this signal depends on. | ||
* @param wrapperId Wrapper id to insert. | ||
*/ | ||
public insertRef(wrapperId: string) { | ||
this._refs.add(wrapperId); | ||
} | ||
|
||
/** | ||
* Check if relationship is circular. | ||
* @param wrapperId Wrapper id to check. | ||
* @returns True if relationship is circular, false otherwise. | ||
*/ | ||
public isCycle(wrapperId: string) { | ||
return this._refs.has(wrapperId); | ||
} | ||
} |