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.jsonable_encoder with SQLAlchemy models directly. PR #1987.HTTPConnection (as Request and WebSocket). Useful for sharing app state in dependencies. PR #1827 by @nsidnev.WebSocketDisconnect and add example handling WebSocket disconnections to docs. PR #1822 by @rkbeatss.1.0.0.
0.32.2. This improves maintainability and allows new features.FastAPI and APIRouter:response_model_skip_defaults (use response_model_exclude_unset instead).response_model_exclude from set() to None (as is in Pydantic).encoders.jsonable_encoder:skip_defaults, use instead exclude_unset.exclude from set() to None (as is in Pydantic).encoders.jsonable_encoder remove parameter sqlalchemy_safe.
orm_mode as described in the tutorial: SQL (Relational) Databases.Form, File) without having python-multipart installed.
python-multipart is installed instead of the incorrect multipart (both importable as multipart).json_encoders. PR #1769 by @henrybetts.jsonable_encoder. PR #1754 by @MashhadiNima.Optional parameters. PR #1731 by @MashhadiNima.**extra parameters in FastAPI. PR #1659 by @bharel.0.13.6 to handle a vulnerability when using static files in Windows. PR #1759 by @jamesag26.servers when there's a root_path instead of prefixing all the paths:
FastAPI classes: root_path_in_servers to disable the auto-generation of servers.root_path and servers in Additional Servers.tokenUrl="token" to make sure those examples keep working as-is even when behind a reverse proxy.openapi_prefix. PR #1611 by @bavaria95..gitignore for contributors using Vim. PR #1590 by @asheux.Optional in all the examples in the docs. Original PR #1574 by @chrisngyn, @kx-chen, @YKo20010. Updated and merged PR #1644.response_model_by_alias. PR #1642.not as a JSON Schema instead of a list. PR #1548 by @v-do.servers. PR #1547 by @mikaello.