Skip to content

swagger plugin cause circular dependency when having a field of optional and nullable enumΒ #3519

@boompikachu

Description

@boompikachu

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using @nestjs/swagger plugin to create a DTO, if the field is an optional nullable enum, the error [below] occurs

Error: A circular dependency has been detected (property key: "status"). Please, make sure that each side of a bidirectional relationships are using lazy resolvers ("type: () => ClassType").

From the repo, we see that statusOne and statusTwo works perfectly fine. But if we introduce statusThree, the one with optional and nullable, it cause the swagger to crash (circular dependency error)

export class CreateTaskDto {
  statusOne?: Status;
  statusTwo: Status | null;
  statusThree?: Status | null; // This cause circular dependency error when opening http://localhost:3000/api (swagger)

  @ApiProperty({ enum: Status, nullable: true })
  statusFour?: Status | null; // This works because @ApiProperty will override the swagger plugin
}

Minimum reproduction code

https://github.com/boompikachu/nestjs-swagger-plugin-repro

Steps to reproduce

  1. pnpm install
  2. pnpm start:dev
  3. open http://localhost:3000/api

comment out the statusThree?: Status | null to fix the error

Expected behavior

To not cause circular dependency

Package version

11.2.0

NestJS version

11.1.4

Node.js version

24

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

in

export class NullableDto {
, there's no test case for nullable and optional enum

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions