Skip to content

Give the ability to set any route class when adding a third-party router#5273

Closed
ikharyn wants to merge 3 commits intofastapi:masterfrom
ikharyn:feature/include_router__route_class_override
Closed

Give the ability to set any route class when adding a third-party router#5273
ikharyn wants to merge 3 commits intofastapi:masterfrom
ikharyn:feature/include_router__route_class_override

Conversation

@ikharyn
Copy link

@ikharyn ikharyn commented Aug 16, 2022

No description provided.

@codecov
Copy link

codecov bot commented Aug 16, 2022

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (31f8757) compared to base (cf73051).
Patch has no changes to coverable lines.

❗ Current head 31f8757 differs from pull request most recent head 3e11ae6. Consider uploading reports for the commit 3e11ae6 to get more accurate results

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     
Impacted Files Coverage Δ
fastapi/routing.py 100.00% <ø> (ø)
tests/main.py 100.00% <0.00%> (ø)
tests/utils.py 100.00% <0.00%> (ø)
fastapi/utils.py 100.00% <0.00%> (ø)
fastapi/encoders.py 100.00% <0.00%> (ø)
tests/test_query.py 100.00% <0.00%> (ø)
fastapi/websockets.py 100.00% <0.00%> (ø)
fastapi/concurrency.py 100.00% <0.00%> (ø)
fastapi/applications.py 100.00% <0.00%> (ø)
... and 25 more

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.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 31f8757 at: https://62fba191bc6e9e31ddc8dfd8--fastapi.netlify.app

@iudeen
Copy link
Contributor

iudeen commented Aug 16, 2022

Any context on why this is needed and what would be the impact?

@ikharyn
Copy link
Author

ikharyn commented Aug 17, 2022

Any context on why this is needed and what would be the impact?

The routing of our project has a nested structure.
At the lower level, a router with an overridden __init__ was used. However, when importing, we need to use a different route class.
Because everywhere in the project it is possible to pass the route_class_override parameter, then it would be right to allow it here as well.

@tiangolo tiangolo added the feature New feature or request label Oct 2, 2023
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot closed this Aug 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants