Complete worked example
python
from typing import Annotated
from fastapi import FastAPI, Path, Query
from fastapi.testclient import TestClient
def inspect_example(request):
app = FastAPI()
@app.get("/roles/{role_id}")
def read(role_id: Annotated[int, Path(ge=1)],
limit: Annotated[int, Query(ge=1, le=5, json_schema_extra={"examples": [2]})] = 2):
return {"id": role_id, "limit": limit}
with TestClient(app) as client:
response = client.get(request["path"], params=request.get("query", {}))
body = response.json()
if response.status_code == 422:
body = [{"at": list(e["loc"]), "type": e["type"]} for e in body["detail"]]
return {"status": response.status_code, "body": body}- Reasoning
- FastAPI converts matching URL values and applies the declared constraints before entering the endpoint. Rejected input produces a validation response. The helper reports error types and locations instead of copying a full error body with possibly sensitive input values into a user-facing message. The Query declaration also contains json_schema_extra with an example. That is documentation metadata, while ge and le are runtime bounds. Recent FastAPI versions no longer accept arbitrary extra keyword arguments as an informal schema extension mechanism. Use the supported argument and keep documentation distinct from enforcement.