Python / FastAPI Interview Questions
How do you customise the OpenAPI documentation in FastAPI?
FastAPI auto-generates OpenAPI 3.x specs from your code. You can enrich the docs with metadata, examples, tags, and custom descriptions without any extra tooling.
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from pydantic import BaseModel, Field
# App-level metadata
app = FastAPI(
title="My Inventory API",
description="## Manage your warehouse inventory\n\nSupports CRUD for items and categories.",
version="2.1.0",
contact={"name": "Dev Team", "email": "dev@example.com"},
license_info={"name": "MIT"},
terms_of_service="https://example.com/terms",
docs_url="/docs", # Swagger UI path
redoc_url="/redoc", # ReDoc path
openapi_url="/openapi.json",
)
class Item(BaseModel):
name: str = Field(
...,
examples=["Widget"],
description="Human-readable item name",
)
price: float = Field(
..., gt=0, examples=[9.99],
description="Price in USD, must be positive",
)
@app.post(
"/items",
summary="Create a new inventory item",
description="Creates an item and returns it with its assigned ID.",
response_description="The created item including its database ID",
tags=["inventory"],
status_code=201,
)
def create_item(item: Item) -> Item:
return item
# Disable docs in production (optional)
# app = FastAPI(docs_url=None, redoc_url=None)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
