mirror of https://github.com/tiangolo/fastapi.git
✨ Support Annotated dependencies in FastAPI class
This commit is contained in:
parent
afa7461f28
commit
c9b8f6ff2d
|
|
@ -333,7 +333,7 @@ class FastAPI(Starlette):
|
|||
),
|
||||
] = None,
|
||||
dependencies: Annotated[
|
||||
Optional[Sequence[Depends]],
|
||||
Optional[Sequence[Depends | Any]],
|
||||
Doc(
|
||||
"""
|
||||
A list of global dependencies, they will be applied to each
|
||||
|
|
@ -345,11 +345,12 @@ class FastAPI(Starlette):
|
|||
**Example**
|
||||
|
||||
```python
|
||||
from typing import Annotated
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from .dependencies import func_dep_1, func_dep_2
|
||||
|
||||
app = FastAPI(dependencies=[Depends(func_dep_1), Depends(func_dep_2)])
|
||||
annotated_dep=Annotated[str,lambda:"annotated"]
|
||||
app = FastAPI(dependencies=[Depends(func_dep_1), Depends(func_dep_2),annotated_dep])
|
||||
```
|
||||
"""
|
||||
),
|
||||
|
|
|
|||
|
|
@ -671,7 +671,7 @@ class APIRoute(routing.Route):
|
|||
return match, child_scope
|
||||
|
||||
|
||||
def get_api_router_dep(dep: params.Depends | Any) -> params.Depends:
|
||||
def get_depends_from_annotated(dep: params.Depends | Any) -> params.Depends:
|
||||
if isinstance(dep, params.Depends):
|
||||
return dep
|
||||
d = analyze_param(
|
||||
|
|
@ -948,7 +948,9 @@ class APIRouter(routing.Router):
|
|||
)
|
||||
self.prefix = prefix
|
||||
self.tags: List[Union[str, Enum]] = tags or []
|
||||
self.dependencies = [get_api_router_dep(dep) for dep in dependencies or []]
|
||||
self.dependencies = [
|
||||
get_depends_from_annotated(dep) for dep in dependencies or []
|
||||
]
|
||||
self.deprecated = deprecated
|
||||
self.include_in_schema = include_in_schema
|
||||
self.responses = responses or {}
|
||||
|
|
|
|||
Loading…
Reference in New Issue