Skip to content

Conversation

@hyeonss0417
Copy link
Contributor

Summary

Closes #94

This PR adds support for @ApiBearerAuth and per-operation security in NestJS mode.

Changes

Security Decorators Support

  • @ApiBearerAuth(name?) - Bearer token authentication
  • @ApiBasicAuth(name?) - Basic authentication
  • @ApiOAuth2(scopes[], name?) - OAuth2 with scopes
  • @ApiSecurity(name, scopes?) - Custom security scheme

Custom Auth Decorators via Config

Added authDecorators config option to support composite decorators:

{
  "nestjs": true,
  "openapi": {
    "securityDefinitions": {
      "bearerAuth": { "type": "http", "scheme": "bearer" }
    },
    "authDecorators": {
      "Auth": "bearerAuth",
      "AdminAuth": "bearerAuth"
    }
  }
}

Usage Example

@Controller('users')
export class UserController {
  @Get('me')
  @ApiBearerAuth('bearerAuth')  // ✅ Now recognized
  async getCurrentUser(): Promise<User> { }

  @Get('protected')
  @Auth()  // ✅ Custom decorator recognized via authDecorators config
  async getProtectedData(): Promise<Data> { }
}

Generated OpenAPI

paths:
  /users/me:
    get:
      security:
        - bearerAuth: []

Files Changed

  • packages/tspec/src/types/tspec.ts - Added authDecorators option type
  • packages/tspec/src/nestjs/types.ts - Added security field to method metadata
  • packages/tspec/src/nestjs/parser.ts - Added security decorator parsing logic
  • packages/tspec/src/nestjs/openapiGenerator.ts - Added security field to operations
  • packages/tspec/src/generator/index.ts - Pass authDecorators to parser
  • docs/guide/nestjs-integration.md - Added Security Decorators documentation

Tests

  • ✅ All 134 tests passing
  • Added tests for @ApiBearerAuth, @ApiBasicAuth, @ApiOAuth2, @ApiSecurity
  • Added tests for custom auth decorators via authDecorators config

- Add support for @ApiBearerAuth, @ApiBasicAuth, @ApiOAuth2, @apisecurity decorators
- Add authDecorators config option for custom composite decorators
- Update NestJS integration documentation with security decorators section
- Add comprehensive tests for security decorator parsing
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NestJS] Support for @ApiBearerAuth and per-operation security in NestJS mode

2 participants