1

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?

Ruslan Plastun
  • 1,985
  • 3
  • 21
  • 48
  • 1
    One of the issue says use it like ```@ApiProperty({ type: (type) => DepartmentDto, isArray: true }) subDepartments: DepartmentDto[];``` but it is not working for me. link=https://github.com/labibramadhan/swagger/pull/1 – Ahmed Nawaz Khan Mar 25 '22 at 11:08

1 Answers1

2

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.