Home > Back-end >  NestJS TypeORM Optional Query not working
NestJS TypeORM Optional Query not working

Time:05-06

I have a problem that is I have set Query Params to optional, but it's not reflecting optional in swagger,

Here is my code:

    @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
  }

But is swagger, it shows like this:

enter image description here

The page and limit and not optional, but for other query parameters must be optional, what Am I missing here?

Thank you

CodePudding user response:

You should add @ApiQuery tag from @nestjs/swagger package and pass in required: false parameter for the optional fields.

  @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
  }
  • Related