Skip to content

✨ Add support for custom APIRoute and APIRouter #13483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

Purushot14
Copy link

πŸš€ feat: Support Custom APIRoute and APIRouter in FastAPI

Summary

This PR introduces support for specifying custom APIRoute and APIRouter classes in FastAPI. It enhances flexibility by allowing users to override the default route and router behavior for advanced use cases.

Changes Introduced

  • Added route_class and router_class parameters to the FastAPI class.
  • Ensured that router_class is used when initializing the app’s router and webhooks.
  • Updated the initialization of self.router to use router_class, passing route_class as an argument.
  • Added test cases to validate:
    • Custom route and router class usage.
    • Route naming consistency.
    • The presence of X-Response-Time header in responses.

Why This is Needed

  • Enables developers to define and use custom route handlers and routers, making FastAPI more extensible.
  • Useful for adding cross-cutting concerns like request logging, timing, or permission handling at the router level.

Testing & Validation

  • Added automated test cases in docs_src.custom_api_router.tutorial001.
  • Verified that route names, router classes, and custom headers behave as expected.

Related Issues or Discussions

(If applicable, link related issues or discussions here)

Purushot14 and others added 4 commits March 13, 2025 00:09
- Introduced `route_class` and `router_class` parameters to allow custom route and router classes.
- Updated `FastAPI` initialization to use the provided `router_class` and `route_class`.
- Modified webhook initialization to respect the custom router class.
- Added test cases to validate:
  - Custom `route_class` and `router_class` usage.
  - Route naming consistency.
  - Presence of `X-Response-Time` header in responses.

This enhances FastAPI’s flexibility, allowing users to customize route handling and routing behavior.
Copy link
Contributor

Copy link
Contributor

Copy link
Contributor

@alv2017
Copy link
Contributor

alv2017 commented Mar 12, 2025

@Purushot14: great idea, I will definitely try this out! πŸ˜ƒ

If it is not get accepted, you can try an alternative way that uses custom_app() πŸ˜ƒ

@svlandeg svlandeg changed the title πŸš€ feat: Add support for custom APIRoute and APIRouter in FastAPI ✨ Add support for custom APIRoute and APIRouter Mar 13, 2025
@svlandeg svlandeg added the feature New feature or request label Mar 13, 2025
Purushot14 and others added 3 commits March 13, 2025 18:53
Copy link
Contributor

Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
Copy link
Contributor

Copy link
Contributor

Copy link
Contributor

github-actions bot commented Apr 5, 2025

Copy link
Contributor

Copy link
Contributor

@Purushot14
Copy link
Author

@svlandeg, do you have any updates on this? Can we merge this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy