🛠 OpenAPI 3.0 & Pydantic Schema Generation
RustAPI provides automatic, 100% FastAPI-compatible OpenAPI 3.0.0 specification generation served at /openapi.json and rendered interactively in Swagger UI at /docs and Redoc at /redoc.
✨ Architectural Advantages
- Zero-Copy Ref Resolution: Pydantic models automatically register schemas in
components["schemas"]and link via$ref. - Nested Hoisting: Pydantic v2
$defsnested schemas are automatically hoisted tocomponents["schemas"]. - Standard 422 Payload: Standardized
HTTPValidationErrorandValidationErrorschemas on all endpoints. - Interactive Swagger Pickers: Single and multi-file array upload pickers for Swagger UI.
- Dependency Isolation:
Depends(...)objects are excluded from query parameters without crashing JSON serializers.
📦 Pydantic Request & Response Models
Decorate route handlers with response_model or use Pydantic type annotations for request body parameters:
from pydantic import BaseModel
from rustapi import FastAPI
app = FastAPI(title="Product Catalog API")
class ProductIn(BaseModel):
name: str
price: float
class ProductOut(BaseModel):
id: int
name: str
price: float
@app.post("/products", response_model=ProductOut, status_code=201)
def create_product(product: ProductIn):
return ProductOut(id=1, name=product.name, price=product.price)
⚠️ Validation Error Schemas (422)
When query, path, or request body payloads fail type validation, RustAPI yields structured 422 Unprocessable Entity JSON matching FastAPI:
{
"detail": [
{
"loc": ["query", "item_id"],
"msg": "invalid integer value",
"type": "type_error.integer"
}
]
}
📁 UploadFile & Form Schemas
from typing import List
from rustapi import FastAPI, File, Form, UploadFile
app = FastAPI()
@app.post("/upload")
def upload_single(file: UploadFile = File(...), note: str = Form("")):
return {"filename": file.filename, "note": note}
@app.post("/upload-batch")
def upload_multi(files: List[UploadFile] = File(...)):
return {"count": len(files)}
🔧 Standalone OpenAPI Utility
from rustapi.openapi.utils import get_openapi
spec = get_openapi(
title="Custom Microservice",
version="1.0.0",
description="Standalone OpenAPI 3.0 spec generator",
)