Intermediate LevelREST APIs & Web Services
Compare Schema-First API Design vs Code-First API Generation.
❓ Interview Prompt: Compare Schema-First (writing OpenAPI YAML before coding) vs Code-First (writing code annotations to generate OpenAPI specs), team alignment, and maintenance.
Architectural strategies for managing API specifications:
1. Schema-First Design (Recommended for Public APIs):
•
Write 'openapi.yaml' contract before writing backend code.
•
Pros: Frontend and backend teams align early; enables instant mock servers and contract testing.
2. Code-First Generation (Recommended for Rapid Internal Microservices):
•
Write code controllers with decorator annotations (e.g. NestJS, Spring Boot), automatically outputting 'swagger.json' at build time.
•
Pros: Single source of truth in code; specs never fall out of sync with code implementation.
Code Implementation Example
typescript
import { ApiProperty } from '@nestjs/swagger';
// Code-First Swagger annotation in NestJS DTO
export class CreateUserDto {
@ApiProperty({ example: 'alice@example.com', description: 'User email' })
email: string;
}💡Code-First OpenAPI annotation defining DTO schemas in NestJS.
🎯 Interview Key Takeaway
Use Schema-First design for cross-team alignment on public APIs; use Code-First generation for rapid internal microservice development.
#Schema-First#Code-First#OpenAPI#Intermediate
Back to all REST APIs & Web Services Questions→