How to add summary and body manually in swagger nestjs

Istiyak Tailor picture Istiyak Tailor · Feb 7, 2020 · Viewed 10.1k times · Source

I am trying to add summary in my swagger documentation routes but I am not able to find the appropriate decorator for defining the summary.

There are some routes in which I have not specified any DTO's. So, I would like to manually add request body for that endpoint.

user.controller.ts

@Controller('users')
@ApiTags('User')
@ApiBearerAuth()
export class UsersController {

  constructor(private readonly service: UsersService) {}

  @Get()
  async findAll() {
    const data = await this.service.findAll();

    return {
      statusCode: 200,
      message: 'Users retrieved successfully',
      data,
    };
  }
}

Swagger

auth.controller.ts

  @UseGuards(AuthGuard('local'))
  @Post('login')
  @ApiParam({
    name: 'email',
    type: 'string'
  })
  @ApiParam({
    name: 'password',
    type: 'string'
  })

  async login(@Request() req) {
    return this.authService.login(req.user);
  }

Answer

Chau Tran picture Chau Tran · Feb 10, 2020

For Endpoint Summary, you can use @ApiOperation(). For schema, you can use @ApiResponse()

@Get()
@ApiOperation({ summary: 'summary goes here' })
@ApiResponse({ status: 200, description: 'description goes here', schema: { ...define schema here... } })
async findAll() {}

Read more about Raw Definitions from the documentations here: https://docs.nestjs.com/recipes/swagger#raw-definitions