Asd*_*567 4 node.js swagger typeorm nestjs
我有一个问题,我已将查询参数设置为可选,但它没有反映 swagger 中的可选,
这是我的代码:
@Get('pagination')
@ApiOperation({ summary: 'Get Activity Post Pagination Enabled' })
public async getActivityPostPagination(
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number,
@Query('user_id') user_id?: string,
@Query('badge_id') badge_id?: string,
@Query('title') title?: string,
) {
//code here
}
Run Code Online (Sandbox Code Playgroud)
但很招摇,它显示如下:
page 和 limit 不是可选的,但对于其他查询参数必须是可选的,我在这里缺少什么?
谢谢
小智 7
您应该从 @nestjs/swagger 包添加 @ApiQuery 标签,并为可选字段传入 required: false 参数。
@Get('pagination')
@ApiOperation({ summary: 'Get Activity Post Pagination Enabled' })
@ApiQuery({ name: 'user_id', required: false, type: String })
@ApiQuery({ name: 'badge_id', required: false, type: String })
public async getActivityPostPagination(
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number,
@Query('user_id') user_id?: string,
@Query('badge_id') badge_id?: string,
@Query('title') title?: string,
) {
//code here
}
Run Code Online (Sandbox Code Playgroud)