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 to app.include_router() or router.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 the APIRouter.
In FastAPI
This allows setting the (mostly new) parameters (additionally to the already existing parameters):
default_response_class: updated to handle defaults in APIRouter and include_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:
from fastapi import FastAPI, Dependsasync def some\_dependency(): returnapp = FastAPI(dependencies=[Depends(some\_dependency)])
In APIRouter
This allows setting the (mostly new) parameters (additionally to the already existing parameters):
default_response_class: updated to handle defaults in APIRouter and include_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 with include_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 calling include_router.tags: OpenAPI tags to apply to all the path operations in this router.For example:
from fastapi import APIRouter, Dependsasync def some\_dependency(): returnrouter = APIRouter(prefix="/users", dependencies=[Depends(some\_dependency)])
In include_router
π Most of these settings are now supported in APIRouter, which normally lives closer to the related code, so it is recommended to use APIRouter when possible.
But include_router is still useful to, for example, adding options (like dependencies, prefix, and tags) when including a third party router, or a generic router that is shared between several projects.
This PR allows setting the (mostly new) parameters (additionally to the already existing parameters):
default_response_class: updated to handle defaults in APIRouter and FastAPI.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 in include_router.
tags in include_router and path operations was updated for consistency, but it's a simple order change.route.response_class, or the router.default_response_class, or the app.default_response_class: the default value for response_class in APIRoute and for default_response_class in APIRouter and FastAPI is now a DefaultPlaceholder used internally to handle and solve default values and overrides. The actual response class inside the DefaultPlaceholder is available at route.response_class.value.β‘οΈ PR #2434 (above) includes new or updated docs:
π± π Add FastAPI monitoring blog post to External Links. PR #2324 by @louisguitton.
π± βοΈ Fix typo in Deta tutorial. PR #2320 by @tiangolo.
π± β¨ Add Discord chat. PR #2322 by @tiangolo.
π± π Fix image links for sponsors. PR #2304 by @tiangolo.
content_type typo. PR #2135 by @TeoZosa.