Show recursivity in swagger docs

Viewed 122

If I declare DepartmentDto type that recursively references itself:

export class DepartmentDto {
  @IsNotEmpty()
  @IsString()
  @ApiProperty()
  id: string;

  @IsNotEmpty()
  @IsString()
  @ApiProperty()
  name: string;

  @ApiProperty()
  parentDepartment: DepartmentDto;

  @Type(() => DepartmentDto)
  @ApiProperty({ type: DepartmentDto, isArray: true })
  subDepartments: DepartmentDto[];
}

I get this inside swagger docs:

[
  {
    "id": "string",
    "name": "string",
    "parentDepartment": "string",
    "subDepartments": [
      "string"
    ]
  }
]

Is there a way to showcase recursivity in the docs using @nestjs/swagger?

1 Answers

The only way I found so far is:

export class DepartmentDto {
  @IsNotEmpty()
  @IsString()
  @ApiProperty()
  id: string;

  @IsNotEmpty()
  @IsString()
  @ApiProperty()
  name: string;

  @ApiProperty()
  parentDepartment: DepartmentDto;

  @ApiProperty({ })
  subDepartments: DepartmentDtoNextlevel[];
}

export class DepartmentDtoNextlevel extends DepartmentDto {}

It will render in swagger at least one child, but at the end, an array of strings will appear inner it:

"subDepartments": [
      "string"
 ]
  • the use of type: () => [DepartmentDtoNextlevel], didnt make difference.
Related