8000 ✨ Add support for shared/top-level parameters (dependencies, tags, etc) by tiangolo · Pull Request #2434 · fastapi/fastapi · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
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

Merged
merged 15 commits into from
Nov 29, 2020

Conversation

tiangolo
Copy link
Member
@tiangolo tiangolo commented Nov 29, 2020

✨ Add support for shared/top-level parameters (dependencies, tags, etc) that can be set in FastAPI, APIRouter, and include_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 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, Depends


async def some_dependency():
    return


app = 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, Depends


async def some_dependency():
    return


router = APIRouter(prefix="/users", dependencies=[Depends(some_dependency)])

In include_router

This 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.


Related to: #1053, #1054

This would supersede/include #1013

…_router

including: prefix, tags, dependencies, deprecated, include_in_schema, responses, default_response_class, callbacks
and showcase them in APIRouter, FastAPI, include_router
in top-level FastAPI, path operations, include_router, APIRouter, its path operations, nested include_router, nested APIRouter, and its path operations
@codecov
Copy link
codecov bot commented Nov 29, 2020

Codecov Report

Merging #2434 (b24d3b6) into master (cc99e23) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master     #2434    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          240       242     +2     
  Lines         7101      7396   +295     
==========================================
+ Hits          7101      7396   +295     
Impacted Files Coverage Δ
tests/test_include_router_defaults_overrides.py 100.00% <ø> (ø)
fastapi/applications.py 100.00% <100.00%> (ø)
fastapi/datastructures.py 100.00% <100.00%> (ø)
fastapi/openapi/utils.py 100.00% <100.00%> (ø)
fastapi/routing.py 100.00% <100.00%> (ø)
tests/test_datastructures.py 100.00% <100.00%> (ø)
tests/test_sub_callbacks.py 100.00% <100.00%> (ø)
...est_tutorial/test_bigger_applications/test_main 8000 .py 100.00% <100.00%> (ø)
...est_tutorial/test_dependencies/test_tutorial012.py 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d550738...b24d3b6. Read the comment docs.

@github-actions
Copy link
Contributor
github-actions bot commented Nov 29, 2020

@github-actions
Copy link
Contributor

📝 Docs preview for commit 80dca8e at: https://5fc3d7036132441645f965c7--fastapi.netlify.app

@github-actions github-actions bot temporarily deployed to commit November 29, 2020 17:31 Inactive
@github-actions
Copy link
Contributor

📝 Docs preview for commit b24d3b6 at: https://5fc3daed330a58d9eaac2e6f--fastapi.netlify.app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0