Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change query parameters serialization using nestjs/swagger?

Recently I have updated nestjs/swagger package in my project to ^4.0.0. Previously Swagger serialized my query parameters as follows:

/resources?parameter=1,2,3

Now it looks like this:

/resources?parameter=1&parameter=2&parameter=3

DTO object for my query looks like this:

class QueryDTO {
  @ApiProperty({
    required: false,
    type: [Number],
  })
  @IsOptional()
  readonly parameter?: number[];
}

How can I change this behaviour?

like image 489
always_bloo Avatar asked Jan 26 '23 08:01

always_bloo


1 Answers

I am on nestjs/swagger 4.5.9

I made it work by define the DTO ( notice the format: 'form')

  @IsNotEmpty()
  @ApiProperty({
    type: [Number],
    format: 'form',
  })
  @IsArray()
  @Transform((value: string) => value.split(',').map(item => Number(item)))
  @IsNumber({}, {each: true})
  deviceId: Array<number>;
like image 195
Huantao Avatar answered Jan 30 '23 01:01

Huantao