पाठ 21 / 25
OpenAPI in Other Frameworks
See how FastAPI, NestJS and ASP.NET Core produce or consume OpenAPI documents.
Most modern frameworks speak OpenAPI
Many frameworks generate OpenAPI from code. FastAPI (Python) builds an OpenAPI 3.1 document from type hints and Pydantic models automatically, serving it at /openapi.json with Swagger UI at /docs and Redoc at /redoc; response models, status codes and examples come from decorators. NestJS (TypeScript) uses the @nestjs/swagger module with decorators such as @ApiProperty and @ApiResponse. ASP.NET Core includes built-in OpenAPI document generation in recent versions through Microsoft.AspNetCore.OpenApi (AddOpenApi() and MapOpenApi()), with Swashbuckle and NSwag as long-standing alternatives. Go has generators such as oapi-codegen that go the other way, generating server interfaces and types from a design-first document. The same principles apply everywhere: annotate thoughtfully (descriptions, examples, error responses), export the document in CI, lint it and check for breaking changes, rather than treating the generated document as an afterthought.
FastAPI generating OpenAPI from type hints
Pydantic models become schemas; decorators add responses and metadata.
from enum import Enum
from fastapi import FastAPI, HTTPException, Path
from pydantic import BaseModel, Field
app = FastAPI(title="Orders API", version="2.3.0")
class OrderStatus(str, Enum):
PLACED = "PLACED"
PAID = "PAID"
SHIPPED = "SHIPPED"
class Order(BaseModel):
id: str = Field(examples=["ord_7Fq2LmX0aB9c"])
status: OrderStatus
total: str = Field(pattern=r"^\d+\.\d{2}$", examples=["1499.00"])
@app.get(
"/orders/{order_id}",
response_model=Order,
operation_id="getOrder",
tags=["Orders"],
responses={404: {"description": "Order not found"}},
)
def get_order(order_id: str = Path(description="Order id")) -> Order:
order = repository.find(order_id)
if order is None:
raise HTTPException(status_code=404, detail="Order not found")
return order
# document: /openapi.json Swagger UI: /docs Redoc: /redocExport and commit the generated document
Even in code-first projects, export the OpenAPI document during the build and keep it under review. Diffing it in pull requests shows API changes that a code diff hides.
त्वरित जाँच: In FastAPI, where is the interactive Swagger UI served by default?
- /swagger
- /api-docs
- /docs
- /ui
Answer
/docs — FastAPI serves Swagger UI at /docs and Redoc at /redoc by default.