fastapi/docs/en/docs/tutorial/dependencies/global-dependencies.md
Sebastián Ramírez 313bbe802f
Add support for shared/top-level parameters (dependencies, tags, etc) (#2434)
*  Add Default and DefaultPlaceholder data structures

to handle defaults and overrides

*  Add utils to get values by priority handling DefaultPlaceholders

*  Add support for top-level parameters in FastAPI, APIRouter, include_router

including: prefix, tags, dependencies, deprecated, include_in_schema, responses, default_response_class, callbacks

* ♻️ Update openapi utils to handle DefaultPlaceholder for response_class

* 📝 Update bigger-application example code to use top-level params

and showcase them in APIRouter, FastAPI, include_router

* 📝 Update docs for Bigger Applications, include diagrams, top-level params

* 🔥 Simplify code and docs for callbacks as default_response_class is no longer required

* 📝 Add docs for top-level dependencies, in FastAPI()

* 📝 Add docs reference to top-level dependencies in docs for decorator

*  Update/increase tests for Bigger Applications including shared parameters

*  Add tests for top-level dependencies in FastAPI()

*  Add tests for internal DefaultPlaceholder

*  Update/increase tests for callbacks with top-level parameters

*  Add LOTS of tests covering branches and cases for shared parameters

in top-level FastAPI, path operations, include_router, APIRouter, its path operations, nested include_router, nested APIRouter, and its path operations

* 🎨 Format/reorder parameters for consistency in FastAPI, APIRouter, include_router
2020-11-29 18:32:18 +01:00

1.1 KiB

Global Dependencies

For some types of applications you might want to add dependencies to the whole application.

Similar to the way you can add dependencies to the path operation decorators{.internal-link target=_blank}, you can add them to the FastAPI application.

In that case, they will be applied to all the path operations in the application:

{!../../../docs_src/dependencies/tutorial012.py!}

And all the ideas in the section about adding dependencies to the path operation decorators{.internal-link target=_blank} still apply, but in this case, to all of the path operations in the app.

Dependencies for groups of path operations

Later, when reading about how to structure bigger applications (Bigger Applications - Multiple Files{.internal-link target=_blank}), possibly with multiple files, you will learn how to declare a single dependencies parameter for a group of path operations.