4

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?

2 Answers 2

8

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>;
Sign up to request clarification or add additional context in comments.

Comments

4

As a workaround, you can remove the @ApiProperty from the DTO and use the @ApiQuery decorator on the controller method which has the style and explode options(just keep the same parameter name as the dto property)

    @Get('resources')
    @ApiQuery({
        name: 'parameter',
        required: false,
        explode: false,
        type: Number,
        isArray: true
    })
    getResources(@Query('parameter') parameter?: number[]) {}

You can still use the DTO object as it is for additional parameters that work the usual way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.