You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Whenever using swagger annotations like @ApiProperty() or @ApiPropertyOptional on an constified version of an enum in combination with swc builder, it throws a circular dependency error:
import{ApiPropertyOptional}from'@nestjs/swagger';exportconstMyEnum={FOO: 'FOO',BAR: 'BAR',}asconst;exporttypeMyEnum=(typeofMyEnum)[keyoftypeofMyEnum];exportclassMyDto{
@ApiPropertyOptional()// will result in circular dependency issue, same for @ApiProperty
@IsOptional()someEnum?: MyEnum;}
After building with swc and starting the application and when accessing the swagger api it crashes with a circular dependency error. The error message does not hold enough information . I configured a project which will provoke this error in the steps to reproduce section. Here is the full error message:
[Nest] 69440 - 03/01/2025, 3:12:31 PM ERROR [ExceptionsHandler] Error: A circular dependency has been detected (property key: "FOO"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").
at SchemaObjectFactory.createNotBuiltInTypeReference (/Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:258:19)
at SchemaObjectFactory.createSchemaMetadata (/Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:375:25)
at SchemaObjectFactory.mergePropertyWithMetadata (/Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:186:21)
at /Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:317:43
at Array.forEach ()
at SchemaObjectFactory.createFromObjectLiteral (/Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:301:24)
at SchemaObjectFactory.createSchemaMetadata (/Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:350:50)
at SchemaObjectFactory.mergePropertyWithMetadata (/Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:186:21)
at /Users/andreas.vanhelden/dev/nestjs-swagger-circular-dependency/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:115:35
Current workaround(s) are:
Use different names for the enum Type and the type itself:
import{ApiPropertyOptional}from'@nestjs/swagger';// Here MyEnum is the const itself and the same name is also used for the typeexportconstMyEnum2={FOO: 'FOO',BAR: 'BAR',}asconst;exporttypeMyEnum2Type=(typeofMyEnum2)[keyoftypeofMyEnum2];exportclassMyDto{
@ApiPropertyOptional()
@IsOptional()someEnum?: MyEnum2Type;}
Define the enum explicitly in the ApiPropertyOptional Annotation parameter:
import{ApiPropertyOptional}from'@nestjs/swagger';// Here MyEnum is the const itself and the same name is also used for the typeexportconstMyEnum={FOO: 'FOO',BAR: 'BAR',}asconst;exporttypeMyEnum=(typeofMyEnum)[keyoftypeofMyEnum];exportclassMyDto{
@ApiPropertyOptional({enum: MyEnum)// or @ApiPropertyOptional({ enum: () => MyEnum })
@IsOptional()someEnum?: MyEnum;}
This issue was really hard to detect and it occured only after building and starting the application. There is just a small hint to the enum like ...Error: A circular dependency has been detected (property key: "FOO")...
It is also dangerous since this error only occurs after deploying and starting the application. On our side it was identified after the deployment on qa environment failed.
Scroll up the failing app.e2e-spec.ts look at the circular dependency error.
Expected behavior
Here are several alternatives i would expect (from my favorite solution to the one i would i would at least expect):
Swagger will somehow resolve that on its own. The error does not occur anymore if swagger Annotations are correctly used.
The error occurs at development time, not after building the project with swc or can be at least easily been tested. The error message is improved and is giving more hints like mentioning the related Dto File, the concrete enum.
The error message is improved and is giving more hints like mentioning the related Dto File and the concrete enum.
Currently we wrote an own test like in the steps to reproduce section, which will verify if Swagger could correctly load the Open Api spec. Maybe there are better ways.
Thanks!
Package version
11.0.5
NestJS version
11.0.1
Node.js version
v20.18.0
In which operating systems have you tested?
macOS
Windows
Linux
Other
Only occurs building with swc and using swagger.
The text was updated successfully, but these errors were encountered:
And created an eslint rule to flag it... but the really annoying thing is that the only way to figure out which enum is to blame is to use the debugger. If we passed the property name down the stack to where this check happens we could have a much clearer error, or I guess we could catch the error higher up the stack where we have it and could rethrow with a clearer message.
Is there an existing issue for this?
Current behavior
Whenever using swagger annotations like
@ApiProperty() or @ApiPropertyOptional
on an constified version of an enum in combination withswc
builder, it throws a circular dependency error:After building with
swc
and starting the application and when accessing the swagger api it crashes with a circular dependency error. The error message does not hold enough information . I configured a project which will provoke this error in the steps to reproduce section. Here is the full error message:Current workaround(s) are:
ApiPropertyOptional
Annotation parameter:This issue was really hard to detect and it occured only after building and starting the application. There is just a small hint to the enum like
...Error: A circular dependency has been detected (property key: "FOO")...
It is also dangerous since this error only occurs after deploying and starting the application. On our side it was identified after the deployment on qa environment failed.
Minimum reproduction code
https://github.com/andreasvh-conceto/nestjs-swagger-circular-dependency
Steps to reproduce
git clone [email protected]:andreasvh-conceto/nestjs-swagger-circular-dependency.git
cd nestjs-swagger-circular-dependency
npm i
npm run test:e2e
app.e2e-spec.ts
look at the circular dependency error.Expected behavior
Here are several alternatives i would expect (from my favorite solution to the one i would i would at least expect):
Currently we wrote an own test like in the steps to reproduce section, which will verify if Swagger could correctly load the Open Api spec. Maybe there are better ways.
Thanks!
Package version
11.0.5
NestJS version
11.0.1
Node.js version
v20.18.0
In which operating systems have you tested?
Other
Only occurs building with swc and using swagger.
The text was updated successfully, but these errors were encountered: