Member-only story
Python’s — Swagger — FastAPI
How /docs (Swagger UI) Gets Generated
Before getting into it, how Swagger UI gets generated, please have a look of project structure which I am using.
Nobody wrote a Swagger page for this project. It’s assembled automatically by FastAPI from the exact same route objects you just walked through — routing metadata, response_model, tags, function signatures, docstrings. Here's the full mechanism.
Step 1 — Route registration happens first (at import time)
Every @router.get(...), @router.post(...) decorator in the codebase doesn't just register a URL — it builds a rich object called APIRoute and stores far more than "path → function."
When this decorator runs, FastAPI captures:
- The path:
/stats(later becomes/api/v1/dashboard/statsonce prefixes are applied) - The HTTP method:
GET - The tag:
"Dashboard"(fromAPIRouter(prefix="/dashboard", tags=["Dashboard"])) - The response schema:
DashboardStats— and by extension, the schema ofStatsBlocknested inside it










