Skip to content
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

feat: add custom route on route level #5897

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

Conversation

arkadybag
Copy link

@arkadybag arkadybag commented Jan 18, 2023

fixed: #1174 #4437
The problem:
Sometimes it is nice to have the ability to pass a custom route to a specific endpoint. For example, I need to check headers before processing with the body, but FASTAPI forces me to read the body before.

Currently, I can't do it because route_class_override uses only internally, and need to reimplement a lot of things to solve it. So, my proposal is to let routers override a route class.

I've tried to solve this issue through middlewares but it's not clear to check into middlewares if I should "filter" requests or not based on the path. Another solution is Mount, but it also not solving the problem because it requires creating sub-path for it (the same with include_router).

PS:
I think my PR partially add support for that one(logically) encode/starlette#1649 encode/starlette#1464

@github-actions
Copy link
Contributor

📝 Docs preview for commit fb7ea03 at: https://63c8137d687c8404f5c931d9--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 814d256 at: https://63c8157edcd0ea06de59aa16--fastapi.netlify.app

@yezz123
Copy link
Contributor

yezz123 commented Jan 19, 2023

This really looks cool 🚀

But if you see it from a different perspective, is that it may lead to routing conflicts if the custom routes overlap with previously defined routes.

This can lead to unexpected behavior and make the API more difficult to understand and maintain. Additionally, if the custom routes are not well-designed or validated, they can introduce security vulnerabilities.

@arkadybag
Copy link
Author

@yezz123 That is the same as define custom route https://fastapi.tiangolo.com/advanced/custom-request-and-route/#create-a-custom-gziprequest-class.
The difference is only that you can use it on endpoint-based level. Your thoughts about

not well-designed or validated, they can introduce security vulnerabilities.

the same as for the custom route class and that is from a different scope. As a user I want to have an ability to overwrite it for endpoint, not for router.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 9142338 at: https://63ce986746614a32b6064712--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 432faed at: https://63ce9d10cf2f1b37a4c3277f--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit f2f4c85 at: https://63cfec8dcf915303d8af94c7--fastapi.netlify.app

@tiangolo tiangolo added feature New feature or request investigate labels Oct 2, 2023
@alejsdev alejsdev added p4 and removed investigate labels Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request p4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to use different middleware for different routes/path
4 participants