Typescript Enum array raises CircularDependencyError
tmtron opened this issue · 10 comments
I'm submitting a...
[ ] Regression
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
using an enum array in a typescript object causes this error:
Error: A circular dependency has been detected (property key: "roles").
Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
not the same, but maybe related to #507
Expected behavior
no error
Minimal reproduction of the problem with instructions
GitHub Repository to reproduce the issue.
- clone the repo
npm install
npm run start:api
See file: create-cat.dto.ts
export class CreateCatDto {
readonly name: string;
readonly age: number;
readonly breed: string;
/**
* using an enum array causes this exception:
*
* Error: A circular dependency has been detected (property key: "roles").
* Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
*/
readonly roles: RoleEnum[];
}
Environment
Nest version: 7.4.2
For Tooling issues:
- Node version: 14.3.0
- Platform: Windows 10 64-bit
This is very likely caused by your compiler configuration. Can you share a minimum reproduction repository without Nx (that uses plain Nest CLI instead)?
@kamilmysliwiec here is a test-repo that uses nest-cli: nestjs-swagger-902
You are right: when we use nest-cli it works.
Any ideas what the problem could be - or which compiler options to look at?
I managed to get a similar error with nest-cli - but I am not sure if it's the very same error.
- I can reproduce it in branch reproduce_issue_902
- I moved the enum to a separate file and now it is imported by
cat.class.ts
andcreate-cat.dto.ts
Error:
Error: A circular dependency has been detected (property key: "Admin"). Please, mak
e sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
at SchemaObjectFactory.createNotBuiltInTypeReference (D:\dev_no_backup\nestjs-swagger-902\node_modules\
@nestjs\swagger\dist\services\schema-object-factory.js:190:19)
at SchemaObjectFactory.mergePropertyWithMetadata (D:\dev_no_backup\nestjs-swagger-902\node_modules\@nes
tjs\swagger\dist\services\schema-object-factory.js:121:25)
at D:\dev_no_backup\nestjs-swagger-902\node_modules\@nestjs\swagger\dist\services\schema-object-factory
.js:254:43
at Array.forEach (<anonymous>)
at SchemaObjectFactory.createFromObjectLiteral (D:\dev_no_backup\nestjs-swagger-902\node_modules\@nestj
s\swagger\dist\services\schema-object-factory.js:238:24)
at SchemaObjectFactory.mergePropertyWithMetadata (D:\dev_no_backup\nestjs-swagger-902\node_modules\@nes
tjs\swagger\dist\services\schema-object-factory.js:100:25)
at D:\dev_no_backup\nestjs-swagger-902\node_modules\@nestjs\swagger\dist\services\schema-object-factory
.js:67:35
at Array.map (<anonymous>)
at SchemaObjectFactory.exploreModelSchema (D:\dev_no_backup\nestjs-swagger-902\node_modules\@nestjs\swa
gger\dist\services\schema-object-factory.js:66:52)
at ResponseObjectFactory.create (D:\dev_no_backup\nestjs-swagger-902\node_modules\@nestjs\swagger\dist\
services\response-object-factory.js:46:47)
@tmtron I looked at your repo and was able to run with the following changes:
export class Cat {
@ApiProperty({
example: 'Maine Coon',
description: 'The breed of the Cat',
required: true,
isArray: true,
enum: RoleEnum,
})
readonly roles?: RoleEnum[];
}
I don't know where you get type: () => [RoleEnum]
from for enum
field. That syntax is specifically for nested classes. Can you make that same change in your code to see if it works?
@nartc thanks - that was a stupid newbie mistake (the error message was not very helpful).
I've updated both repos (to fix the wrong use of type
) and I still see the original issue:
- when using webpack we get the
Circular Dependency
error: see this GitHub Repo "nx-swagger" - when using the nest-cli the same code works: GitHub Repo "nestjs-swagger-902"
@tmtron are you still seeing issues with nest-cli
project with the code change? Or is it just with Nx now?
@nartc the nest-cli version works now.
The nx example with the nestjs-swagger-plugin executed by webpack doesn't.
@tmtron unfortunately if it's Nx related now, you're going to have to raise an issue over at Nx repo because we cannot fix/maintain what they have for their NodeJS custom builders. So I'd suggest closing this issue as it would be categorized as won't fix.
Anyway, have you checked out this issue? nrwl/nx#2147 . It basically asks for support custom compiler plugins (which is exactly what Swagger Plugin is). Although still open, it does have a lot of good conversation as well as workarounds you might try out for your case. Good luck!
@nartc You are right: it is an nx issue. I'll close this, since it works in the nest-cli.
Follow-up: I managed to workaround the issue using this comment: