Give the ability to set any route class when adding a third-party router#5273
Give the ability to set any route class when adding a third-party router#5273ikharyn wants to merge 3 commits intofastapi:masterfrom
Conversation
Codecov ReportBase: 100.00% // Head: 100.00% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## master #5273 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 540 532 -8
Lines 13969 13684 -285
==========================================
- Hits 13969 13684 -285
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
|
📝 Docs preview for commit 31f8757 at: https://62fba191bc6e9e31ddc8dfd8--fastapi.netlify.app |
|
Any context on why this is needed and what would be the impact? |
The routing of our project has a nested structure. |
YuriiMotov
left a comment
There was a problem hiding this comment.
@ilyaTT, thanks for contributing to FastAPI!
As I understand it, the idea is to provide the way to write
router = APIRouter()
main_router.include_router(router=router, route_class_override=MyAPIRoute)instead of
router = APIRouter(route_class=MyAPIRoute)
main_router.include_router(router=router)full code in details
Details
from typing import Callable
from fastapi import APIRouter, FastAPI, Request, Response
from fastapi.routing import APIRoute
class MyAPIRoute(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
print("MyAPIRoute called")
return await original_route_handler(request)
return custom_route_handler
# router = APIRouter(route_class=MyAPIRoute)
router = APIRouter()
@router.get("/")
async def get_():
pass
main_router = APIRouter()
# main_router.include_router(router=router)
main_router.include_router(router=router, route_class_override=MyAPIRoute)
app = FastAPI()
app.include_router(router=main_router)Then, why don't we add the same route_class_override to FastAPI class?
Also, we need to add description for route_class_override parameter and tests.
|
As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR. |
No description provided.