Nestjs swagger integration

From Intense-Visions/harness-engineering · View interactive version

Nestjs swagger integration is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Document APIs with @ApiProperty, @ApiOperation, @ApiTags, and DocumentBuilder

How to use this skill

Skill file

NestJS Swagger Integration

Document APIs with @ApiProperty, @ApiOperation, @ApiTags, and DocumentBuilder

When to Use

Instructions

  1. Install and bootstrap:
npm install @nestjs/swagger swagger-ui-express
// main.ts
const config = new DocumentBuilder()
  .setTitle('My API')
  .setDescription('REST API documentation')
  .setVersion('1.0')
  .addBearerAuth()
  .build();

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document, {
  swaggerOptions: { persistAuthorization: true },
});
  1. Annotate controllers:
@ApiTags('users')
@Controller('users')
export class UsersController {
  @ApiOperation({ summary: 'Get a user by ID' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  @ApiResponse({ status: 200, type: UserDto })
  @ApiResponse({ status: 404, description: 'User not found' })
  @Get(':id')
  findOne(@Param('id') id: string) { ... }

  @ApiOperation({ summary: 'Create a user' })
  @ApiBody({ type: CreateUserDto })
  @ApiResponse({ status: 201, type: UserDto })
  @ApiBearerAuth()
  @Post()
  create(@Body() dto: CreateUserDto) { ... }
}
  1. Annotate DTOs:
export class CreateUserDto {
  @ApiProperty({ example: 'user@example.com', description: 'User email address' })
  @IsEmail()
  email: string;

  @ApiProperty({ example: 'myP@ssw0rd', minLength: 8 })
  @IsString()
  @MinLength(8)
  password: string;

  @ApiPropertyOptional({ example: 'John Doe' })
  @IsOptional()
  @IsString()
  displayName?: string;
}
  1. Use mapped types from @nestjs/swagger (not @nestjs/mapped-types) for full Swagger compatibility:
import { PartialType, OmitType, PickType } from '@nestjs/swagger';
export class UpdateUserDto extends PartialType(CreateUserDto) {}
export class UserProfileDto extends OmitType(CreateUserDto, ['password']) {}
  1. Document pagination responses:
@ApiResponse({ status: 200, schema: {
  properties: {
    data: { type: 'array', items: { $ref: getSchemaPath(UserDto) } },
    total: { type: 'number' },
    page: { type: 'number' },
  }
}})

Details

@nestjs/swagger uses TypeScript reflection metadata to auto-detect types in DTOs and controllers. When emitDecoratorMetadata is enabled in tsconfig.json (it must be for NestJS to work), most types are inferred automatically.

Enum documentation: Pass enum: MyEnum in @ApiProperty for Swagger to show all valid values: @ApiProperty({ enum: UserRole, enumName: 'UserRole' }).

@ApiHideProperty(): Exclude a property from the Swagger spec (e.g., internal tracking fields) without removing it from the DTO class.

CLI plugin (recommended): Add to nest-cli.json to auto-add @ApiProperty based on class-validator decorators — eliminates duplication:

{
  "compilerOptions": {
    "plugins": ["@nestjs/swagger"]
  }
}

Exclude from production: The Swagger UI and spec endpoint should be conditionally registered. Wrap the SwaggerModule.setup() call: if (process.env.NODE_ENV !== 'production') { ... }.

Export the spec: SwaggerModule.createDocument() returns a plain OpenAPI 3.0 object. Write it to disk with fs.writeFileSync('swagger.json', JSON.stringify(document)) to integrate with API gateway import workflows.

Source

https://docs.nestjs.com/openapi/introduction

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.