0
0
NestJSframework~20 mins

Swagger API documentation in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swagger Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Swagger setup in NestJS?
Given this NestJS Swagger setup, what will the generated API documentation show for the GET /cats endpoint?
NestJS
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'];
  }
}
AThe Swagger UI will show a 'cats' section with a GET /cats endpoint labeled 'List all cats'.
BThe Swagger UI will show no endpoints because @ApiTags is missing.
CThe Swagger UI will show a POST /cats endpoint instead of GET.
DThe Swagger UI will show the GET /cats endpoint but with no description.
Attempts:
2 left
💡 Hint
Look at the decorators @ApiTags and @ApiOperation and what they do.
📝 Syntax
intermediate
2:00remaining
Which option correctly adds a query parameter to Swagger docs in NestJS?
You want to document a query parameter 'limit' of type number for a GET endpoint using Swagger decorators in NestJS. Which option correctly adds this parameter?
NestJS
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 [];
  }
}
A@ApiQuery('limit')
B@ApiQuery({ name: 'limit', type: 'string', required: true })
C@ApiQuery({ name: 'limit', type: Number, required: false })
D@ApiParam({ name: 'limit', type: Number })
Attempts:
2 left
💡 Hint
Check the correct decorator for query parameters and the type property.
🔧 Debug
advanced
2:00remaining
Why does this Swagger setup not show the expected response schema?
This NestJS controller method uses @ApiResponse but Swagger UI does not show the response schema. What is the cause?
NestJS
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 }];
  }
}
AThe Cat class lacks decorators like @ApiProperty, so Swagger cannot generate schema.
BThe return type must be an array of Cat, but type: Cat is used.
CThe controller method must be async to show response schema.
DThe @ApiResponse decorator is used incorrectly; it should be @ApiOkResponse.
Attempts:
2 left
💡 Hint
Swagger needs metadata on class properties to generate schemas.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of the SwaggerModule in NestJS?
In NestJS, what does the SwaggerModule do when setting up API documentation?
AIt automatically generates API controllers without writing code.
BIt creates and serves the Swagger UI and JSON docs based on decorators in the code.
CIt replaces the NestJS routing system with Swagger routes.
DIt validates incoming API requests against Swagger specs at runtime.
Attempts:
2 left
💡 Hint
Think about how SwaggerModule interacts with your NestJS app and decorators.
state_output
expert
2:00remaining
What is the output of this NestJS Swagger setup with multiple tags and global prefix?
Consider this NestJS app setup with Swagger and a global prefix. What URL will serve the Swagger UI?
NestJS
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();
ASwagger UI will not be available because of the global prefix
BSwagger UI will be available at http://localhost:3000/api/v1/docs
CSwagger UI will be available at http://localhost:3000/api/v1/swagger
DSwagger UI will be available at http://localhost:3000/docs
Attempts:
2 left
💡 Hint
Check how SwaggerModule.setup path relates to global prefix.