Fix openapi document with dependencies override (#5451)#5452
Fix openapi document with dependencies override (#5451)#5452lmignon wants to merge 5 commits intofastapi:masterfrom
Conversation
|
📝 Docs preview for commit aed607d at: https://6339f3a6e3f39c274895ddee--fastapi.netlify.app |
aed607d to
acf37ee
Compare
|
📝 Docs preview for commit acf37ee at: https://6339f80a0dfefb27382a1ac6--fastapi.netlify.app |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5452 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 540 542 +2
Lines 13969 14003 +34
=========================================
+ Hits 13969 14003 +34 ☔ View full report in Codecov by Sentry. |
|
📝 Docs preview for commit e5396cd at: https://633b40fa63c4ac2ee3f06d9d--fastapi.netlify.app |
e5396cd to
4ad2bc3
Compare
|
📝 Docs preview for commit 4ad2bc3 at: https://633b422f18b80c30ba4d1c06--fastapi.netlify.app |
4ad2bc3 to
ee235fb
Compare
|
📝 Docs preview for commit ee235fb at: https://633b441611b92d32862315ee--fastapi.netlify.app |
ee235fb to
39fb6b4
Compare
|
📝 Docs preview for commit 39fb6b4 at: https://633b45949cb90d34d468a0a3--fastapi.netlify.app |
|
📝 Docs preview for commit 3f7a29c at: https://633c175b5c3f4076c7a5e89e--fastapi.netlify.app |
3f7a29c to
7ba81c5
Compare
|
This PR also includes a commit to pin the version of trio since the last version 0.22.0 breaks tests. A fix is proposed here #5456 |
7ba81c5 to
e31a2c3
Compare
|
📝 Docs preview for commit e31a2c3 at: https://633c19e333bbe9797f5dda49--fastapi.netlify.app |
|
@tiangolo No news about this one? IMO this changes makes a lot of sense but the way I fixed the code is may be not appropriate since that's the first time I dig into it. |
|
📝 Docs preview for commit db3212f at: https://639cda9b2c118d005f47f6ee--fastapi.netlify.app |
db3212f to
b915ccf
Compare
|
📝 Docs preview for commit b915ccf at: https://640b02dc77012078081ced3e--fastapi.netlify.app |
b915ccf to
3b7895b
Compare
|
@tiangolo I rebased this branch on 0.96.0. I hope it will find a path to be included into a next release. |
|
📝 Docs preview for commit 3b7895b at: https://64833db8a9aabd1989f2d478--fastapi.netlify.app |
3b7895b to
cb8573b
Compare
|
rebased on 0.97 |
|
rebased on 0.98 |
38efd0c to
973a53e
Compare
|
I'll adapt the code to work on the 1.X version |
9a1fadd to
344e322
Compare
344e322 to
7f11e98
Compare
61fdc10 to
eb35b5f
Compare
6280f4f to
9ae3c2e
Compare
|
rebased on 0.110.0 |
9ae3c2e to
dc1b214
Compare
75389c4 to
8413c75
Compare
|
rebased on 0.115.0 @tiangolo Any chance to get this reviewed or better merged? It's very useful. Thank you. |
|
@tiangolo I'm also interested! Could you please have a look? Thanks |
|
We use FastAPI's dependency override mechanism to create generic endpoint modules that declare what they need, but not how to get it. This allows us to build reusable API fragments, tailor behavior per deployment, and cleanly separate concerns. It's a lightweight and powerful way to introduce inversion of control in REST API design. 💡 ContextFastAPI currently provides However, We are using this mechanism in production, not for testing, but to enable modular and context-adaptable API composition. This usage pattern has proven extremely powerful in promoting clean architecture, reusable API fragments, and dependency inversion. I would like to propose that this usage be recognized and supported in the documentation — and ideally improved to fully support production use, particularly in relation to OpenAPI schema generation. ✅ Use Case: Building Reusable API FragmentsWe build endpoint modules that:
At application startup, we use # api/user.py
from fastapi import APIRouter, Depends
from .schemas import User
def get_user() -> User:
raise NotImplementedError("Override this in your app")
router = APIRouter()
@router.get("/me")
def get_me(user: User = Depends(get_user)):
return {"username": user.username}
# main.py
from fastapi import FastAPI
from api.user import router as user_router, get_user
from .auth import get_current_user_from_oauth
app = FastAPI()
app.include_router(user_router)
# Runtime context binding
app.dependency_overrides[get_user] = get_current_user_from_oauth💪 Benefits of This Pattern
In short:
This usage aligns closely with dependency injection best practices found in frameworks like Spring, Angular, or ASP.NET Core — and works extremely well within FastAPI's design philosophy.
|
sebastienbeau
left a comment
There was a problem hiding this comment.
Tested, and used in production !
Thanks for your work @lmignon
|
Thanks for the interest and for the detailed explanation! 🍰 The current intention of dependency overrides is mainly for tests. Maybe it would make sense to allow a more general way to allow defining dependency overrides in production, but in that case, I think it would probably need a different interface than what is currently available in FastAPI. This might be related to a large refactor I want to do later with how routers work. Nevertheless, I would get nervous if the overriding dependency is so different that it would generate a different OpenAPI schema, I would think that then it would probably be doing too much, in a very different way, so I would prefer not to have that. The other part is, any new change or feature I accept, I have to keep maintaining it going forward, and fix any and all the possible side effects and potential breaks it could cause. I might revisit this idea in the future, but probably in a different way, after some other reactors and features I need to make. For now, I prefer to pass on this one, but thanks for all the effort put into it! ☕ 🍰 |
refs #5451