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: https://docs.nestjs.com/openapi/types-and-parameters
So if my variable name is features
in code, documentation automatically picks that up. The request fails because features
is not array for single element. If I try to separately add the param to 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;
}
This is how swagger looks with above code
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 in @ApiProperty() decorator above features add name like this @ApiProperty({ name: 'features[]'})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.