swaggernestjsswagger-ui

Nestjs swagger array of strings with one parameter


When I send only one parameter, I got query result like string, not like string[]. This heppend only from UI swagger, if I send from Postman - it works good.

I just want send from swagger-ui one parammeter and got array of string, not string

How I can fix it? Help me please.

Example1: send one paramenter and in my controller I got string like '25'

Example2: when I send 2 parameters in controller I can see array of strings ('25', '21')

export class List {
  @ApiProperty({ isArray: true, type: String, required: false })
  @IsOptional()
  public categories?: string[];
}

Solution

  • when you fill a single category the query param will be translated as a string while when you fill in several categories understand it as a array.

    to solve this problem I added in DTO :

    @Transform(({ value }) => (Array.isArray(value) ? value : Array(value)))

    I force the cast to array