OpenAPI & Documentation
Priority: P2 (MAINTENANCE)
Automated API documentation and OpenAPI standards.
Workflow
- Enable the Swagger plugin in
nest-cli.jsonto auto-generate@ApiPropertyfrom DTOs. - Annotate controllers with
@ApiTags,@ApiResponse, and auth decorators. - Create generic wrappers for paginated and polymorphic responses.
- Generate separate docs for public vs internal audiences.
Setup
See implementation examples for nest-cli.json plugin config and Swagger bootstrap.
Response Documentation
- Strictness: Every controller method must have
@ApiResponse({ status: 200, type: UserDto }). - Generic Wrappers: Define
ApiPaginatedResponse<T>decorators usingApiExtraModels+getSchemaPath()to handle generics properly.
Advanced Patterns
- Polymorphism: Use
@ApiExtraModelsandgetSchemaPathforoneOf/anyOfunion types. - File Uploads: Use
@ApiConsumes('multipart/form-data')with explicit@ApiBodyschema. - Authentication: Use
@ApiBearerAuth()or@ApiSecurity('api-key')matchingDocumentBuilderconfig. - Enums: Force named enums:
@ApiProperty({ enum: MyEnum, enumName: 'MyEnum' }).
Operation Grouping
- Tags: Mandatory
@ApiTags('domains')on every Controller. - Multiple Docs: Generate separate docs for different audiences (Public vs Internal).
Anti-Patterns
- No missing @ApiResponse: Every controller method must declare its response type and status code.
- No /docs in production: Disable Swagger in production to prevent API schema exposure.
- No manual @ApiProperty everywhere: Use the Nest CLI Swagger plugin to auto-generate from DTOs.