-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
✨ Add support for shared/top-level parameters (dependencies, tags, etc) #2434
Conversation
to handle defaults and overrides
…_router including: prefix, tags, dependencies, deprecated, include_in_schema, responses, default_response_class, callbacks
and showcase them in APIRouter, FastAPI, include_router
…o longer required
in top-level FastAPI, path operations, include_router, APIRouter, its path operations, nested include_router, nested APIRouter, and its path operations
Codecov Report
@@ Coverage Diff @@
## master #2434 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 240 242 +2
Lines 7101 7396 +295
==========================================
+ Hits 7101 7396 +295
Continue to review full report at Codecov.
|
🚀 Deployed on https://5fc3dabcda660a7d3c4aa06c--fastapi.netlify.app |
📝 Docs preview for commit 80dca8e at: https://5fc3d7036132441645f965c7--fastapi.netlify.app |
📝 Docs preview for commit b24d3b6 at: https://5fc3daed330a58d9eaac2e6f--fastapi.netlify.app |
✨ Add support for shared/top-level parameters (dependencies, tags, etc) that can be set in
FastAPI
,APIRouter
, andinclude_router
.Up to now, for several options, the only way to apply them to a group of path operations was in
include_router
. That works well, but the call toapp.include_router()
orrouter.include_router()
is normally done in another file.That means that, for example, to apply authentication to all the path operations in a router it would end up being done in a different file, instead of keeping related logic together.
Setting options in
include_router
still makes sense in some cases, for example, to override or increase configurations from a third party router included in an app. But in a router that is part of a bigger application, it would probably make more sense to add those settings when creating theAPIRouter
.In
FastAPI
This allows setting the (mostly new) parameters (additionally to the already existing parameters):
default_response_class
: updated to handle defaults inAPIRouter
andinclude_router
.dependencies
: to include ✨ top-level dependencies ✨ that apply to the whole application. E.g. to add global authentication.callbacks
: OpenAPI callbacks that apply to all the path operations.deprecated
: to mark all the path operations as deprecated. 🤷include_in_schema
: to allow excluding all the path operations from the OpenAPI schema.responses
: OpenAPI responses that apply to all the path operations.For example:
In
APIRouter
This allows setting the (mostly new) parameters (additionally to the already existing parameters):
default_response_class
: updated to handle defaults inAPIRouter
andinclude_router
. For example, it's not needed to set it explicitly when creating callbacks.dependencies
: to include ✨ router-level dependencies ✨ that apply to all the path operations in a router. Up to now, this was only possible withinclude_router
.callbacks
: OpenAPI callbacks that apply to all the path operations in this router.deprecated
: to mark all the path operations in a router as deprecated.include_in_schema
: to allow excluding all the path operations in a router from the OpenAPI schema.responses
: OpenAPI responses that apply to all the path operations in a router.prefix
: to set the path prefix for a router. Up to now, this was only possible when callinginclude_router
.tags
: OpenAPI tags to apply to all the path operations in this router.For example:
In
include_router
This allows setting the (mostly new) parameters (additionally to the already existing parameters):
default_response_class
: updated to handle defaults inAPIRouter
andFastAPI
.deprecated
: to mark all the path operations in a router as deprecated in OpenAPI.include_in_schema
: to allow disabling all the path operations from showing in the OpenAPI schema.callbacks
: OpenAPI callbacks that apply to all the path operations in this router.Note: all the previous parameters are still there, so it's still possible to declare
dependencies
ininclude_router
.Related to: #1053, #1054
This would supersede/include #1013