diff --git a/docs/en/docs/how-to/custom-request-and-route.md b/docs/en/docs/how-to/custom-request-and-route.md index 8ff07077c..a47a64291 100644 --- a/docs/en/docs/how-to/custom-request-and-route.md +++ b/docs/en/docs/how-to/custom-request-and-route.md @@ -128,7 +128,7 @@ This example enhances FastAPI with structured routing and response timing, makin ##### 📌 API Structure - **`/healthz`**: Health check endpoint for the main router. it path name is `Global.health-check`. - **`/model/create`**: Model creation endpoint for the model router with path name `Model.create`. -- **`/model/{model_id}/item/create`**: Item creation endpoint for the item router and its child router of model +- **`/model/{model_id}/item/create`**: Item creation endpoint for the item router and its child router of model router with path name `Model.Item.create`. ##### 🔥 Benefits @@ -138,4 +138,4 @@ This example enhances FastAPI with structured routing and response timing, makin This setup is **ideal for scalable FastAPI projects**, ensuring better organization and easier debugging. -{* ../../docs_src/custom_api_router/tutorial001.py hl[93:120,30:36] *} \ No newline at end of file +{* ../../docs_src/custom_api_router/tutorial001.py hl[93:120,30:36] *} diff --git a/docs_src/custom_api_router/tutorial001.py b/docs_src/custom_api_router/tutorial001.py index 3714812cc..d889c7a52 100644 --- a/docs_src/custom_api_router/tutorial001.py +++ b/docs_src/custom_api_router/tutorial001.py @@ -110,6 +110,7 @@ app = FastAPI(route_class=TimedRoute, router_class=AppRouter) model = AppRouter(prefix="/model", name="Model", route_class=TimedRoute) item = AppRouter(prefix="/{model_id}/item", name="Item", route_class=TimedRoute) + async def create_model(request: Request): """ Create a model @@ -117,12 +118,21 @@ async def create_model(request: Request): print("Model created") route: TimedRoute = request.scope["route"] router: AppRouter = request.scope["router"] - return JSONResponse({"route_class": route.__class__.__name__, "route_name": route.name, "router_class": router.__class__.__name__}, status_code=200) + return JSONResponse( + { + "route_class": route.__class__.__name__, + "route_name": route.name, + "router_class": router.__class__.__name__, + }, + status_code=200, + ) + model.add_api_route( path="/create", endpoint=create_model, methods=["POST"], name="create-model" ) + async def create_item(request: Request): """ Create an item @@ -131,8 +141,14 @@ async def create_item(request: Request): route: TimedRoute = request.scope["route"] router: AppRouter = request.scope["router"] return JSONResponse( - {"route_class": route.__class__.__name__, "route_name": route.name, "router_class": router.__class__.__name__}, - status_code=200) + { + "route_class": route.__class__.__name__, + "route_name": route.name, + "router_class": router.__class__.__name__, + }, + status_code=200, + ) + item.add_api_route( path="/create", endpoint=create_item, methods=["POST"], name="create-item" diff --git a/tests/test_tutorial/test_custom_request_and_route/test_tutorial004.py b/tests/test_tutorial/test_custom_request_and_route/test_tutorial004.py index ca427376b..da451fa8c 100644 --- a/tests/test_tutorial/test_custom_request_and_route/test_tutorial004.py +++ b/tests/test_tutorial/test_custom_request_and_route/test_tutorial004.py @@ -13,22 +13,29 @@ from docs_src.custom_api_router.tutorial001 import app client = TestClient(app) + def test_get_timed(): response = client.get("/healthz") assert response.text == "OK" assert "X-Response-Time" in response.headers assert float(response.headers["X-Response-Time"]) >= 0 + def test_route_class(): - response = client.post("/model/create", json={"name": "test", "description": "test"}) + response = client.post( + "/model/create", json={"name": "test", "description": "test"} + ) assert response.status_code == 200 response_json = response.json() assert response_json["route_name"] == "Global.Model.create-model" assert response_json["route_class"] == "TimedRoute" assert response_json["router_class"] == "AppRouter" + def test_route_name(): - response = client.post("/model/Model001/item/create", json={"name": "test", "description": "test"}) + response = client.post( + "/model/Model001/item/create", json={"name": "test", "description": "test"} + ) assert response.status_code == 200 response_json = response.json() assert response_json["route_name"] == "Global.Model.Item.create-item"