How to disable security for a specific controller method in NestJS/Swagger?

Viewed 874

I am using NestJS with Swagger Module to produce the equivalent API Spec. Is there a way to disable security for a specific controller method, while having marked the Controller class as requiring authentication? Example:

// apply bearer auth security to controller
@ApiBearerAuth()
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  // How can **getHello** method be made public???
  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

I am looking for a more intuitive way compared to the straightforward one where each controller method should be mark with security except for the public ones....

I have tried using @ApiOperation({ security: [] }) without any result. It still get's the security definition from the controller class

2 Answers

It seems after all that this has been already discussed and will not be implemented: github.com/nestjs/swagger/issues/1319

@ApiBearerAuth() support Controller and function. You should put @ApiBearerAuth() into what function you need

// apply bearer auth security to controller
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  // How can **getHello** method be made public???
  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @ApiBearerAuth()  <---- here
  @Post()
  createHello(): string {
    return this.appService.createHello();
  }

}
Related