EDIT: found a solution thanks to the comment suggesting to use a DTO. Answer detailed at the bottom.
The NestJS website has documentation to [declare default values][1] when using @ApiBody(), is there a way to do so with @ApiQuery()? (i.e. show in the documentation that the queries have default values)
for example, if I have pagination queries and want the default to be page 1 with 5 entries per page:
@Get()
@ApiQuery({name: 'page', default: 1, type: Number})
@ApiQuery({name: 'limit', default: 5, type: Number})
async getDocuments(
@Query('page') page: Number = 1,
@Query('limit') limit: Number = 5
){
return this.documentsService.getDocuments(page, limit);
}
Using DTO as suggested in the comments:
The result:
*made a typo, default here says 0 but it should be 1
Furthermore, a single DTO can be used for multiple query parameters. This is especially useful if the same params are being used by multiple functions:
Also note the omission of type declarations on my working examples --this is because if the type is declared, swagger produces duplicate params