import { Controller, Get } from '@nestjs/common'; import { ApiTags, ApiOperation } from '@nestjs/swagger'; @ApiTags('cats') @Controller('cats') export class CatsController { @Get() @ApiOperation({ summary: 'List all cats' }) findAll() { return ['Tom', 'Garfield']; } }
The @ApiTags('cats') decorator groups the controller endpoints under the 'cats' tag in Swagger UI. The @ApiOperation decorator adds a summary description to the GET /cats endpoint. So the Swagger UI will show a 'cats' section with the GET /cats endpoint labeled 'List all cats'.
import { Controller, Get, Query } from '@nestjs/common'; import { ApiQuery } from '@nestjs/swagger'; @Controller('items') export class ItemsController { @Get() // Add query param decorator here findAll(@Query('limit') limit: number) { return []; } }
The @ApiQuery decorator documents query parameters. The type should be Number (the class, not string). Option C correctly uses @ApiQuery({ name: 'limit', type: Number, required: false }). Option C uses wrong type and required true. Option C is invalid syntax. Option C uses @ApiParam which is for path parameters, not query.
import { Controller, Get } from '@nestjs/common'; import { ApiResponse, ApiProperty } from '@nestjs/swagger'; class Cat { @ApiProperty() name: string; @ApiProperty() age: number; } @Controller('cats') export class CatsController { @Get() @ApiResponse({ status: 200, description: 'Success', type: Cat }) findAll() { return [{ name: 'Tom', age: 5 }]; } }
Swagger uses decorators like @ApiProperty on class properties to generate response schemas. Without these, the schema is missing. Option A is incorrect because @ApiResponse can be used with type. Option A is irrelevant. Option A is not mandatory; Swagger can infer arrays but needs property metadata.
The SwaggerModule reads the decorators in your NestJS code and generates the Swagger JSON and UI to document your API. It does not generate controllers, replace routing, or validate requests at runtime.
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.setGlobalPrefix('api/v1'); const config = new DocumentBuilder() .setTitle('My API') .setDescription('API description') .setVersion('1.0') .addTag('users') .addTag('products') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('docs', app, document); await app.listen(3000); } bootstrap();
The global prefix applies to the app routes, but SwaggerModule.setup serves the UI at the exact path given relative to the root. So Swagger UI is at /docs, not under the global prefix /api/v1.