I am using @nestjs/swagger
for API documentation. I want my queryParam to be features[]
and not features
when swagger makes the request.
Nest searches for @Query
in route handlers and documents automatically.
The SwaggerModule searches for all @Body(), @Query(), and @Param() decorators in route handlers to generate the API document. It also creates corresponding model definitions by taking advantage of reflection. from here
So if my variable name is features
in code, documentation automatically picks that up. The request fails because features
is not an array for a single element. If I try to separately add the param to the swagger document using @ApiQuery
, I have the option to name the param features[]
but it shows up separately, and I am unable to remove the automatically added features
param.
@Get('/:uuid/configs')
@ApiOperation({
summary: 'Get configs for user',
})
//manually add param with [] in the name
@ApiQuery({
name: 'features[]',
required: true,
type: [String],
})
async getUserConfigs(
//automatic pickup from here
@Query() queryParams: GetUserConfigQueryParamsDto,
) {
return {
success: true,
} as ResponseBuilder;
}
I also tried to pass the name of the query as I want as a parameter in @Query.
@Get('/:uuid/configs')
@ApiOperation({
summary: 'Get configs for user',
})
//manually add param with [] in the name
@ApiQuery({
name: 'features[]',
required: true,
type: [String],
})
async getUserConfigs(
//Added the desired name as a param
@Query('features[]') queryParams: GetUserConfigQueryParamsDto,
) {
return {
success: true,
} as ResponseBuilder;
}
This changes the name in swagger for sure. But when I execute the request in this case, the [
converts to %5B
and %5D
due to encoding. So the request fails.
I want only features[]
and not both. But I can not remove @Query or add [] to the variable name. How do I get only features[]
?
Here is the GetUserConfigQueryParamsDto
jic:
export class GetUserConfigQueryParamsDto {
@ApiProperty()
@IsDefined()
@IsArray()
@ArrayNotEmpty()
features: string[];
}
In GetUserConfigQueryParamsDto
class at @ApiProperty()
decorator above features add name like this
@ApiProperty({ name: 'features[]'})