Swagger request body format is not working well for nested data structure in nest

313 Views Asked by At

I am creating api using NestJS and making the api documentation using swagger. Following code snippet is showing my dto definition.

export class CreateWoopDto {
  @ApiProperty()
  @IsUUID()
  user_id: string;

  @ApiProperty()
  @ValidateNested({ each: true })
  @Type(() => WoopStatementDto)
  @IsArray()
  content: WoopStatementDto[];
}


export class WoopStatementDto {
  @ApiProperty()
  @IsString()
  if: string;

  @ApiProperty()
  @IsString()
  then: string;
}

In my POST/ createWoop endpoint, I am using this Dto, and in my swagger doc, the request body format for this endpoint is not working as I expected.

enter image description here

How can I show the content field in the correct format here?

1

There are 1 best solutions below

0
Micael Levi On BEST ANSWER

you should declare that content like this:

  @ApiProperty({ type: [WoopStatementDto] })
  // @ApiProperty({ type: WoopStatementDto, isArray: true }) 
  @ValidateNested({ each: true })
  @Type(() => WoopStatementDto)
  @IsArray()
  content: WoopStatementDto[];