Skip to content

Commit

Permalink
馃悰 Allow async class methods as dependencies not decorated
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Collier committed Sep 15, 2023
1 parent c81e136 commit 5812a5c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fastapi/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ async def app(request: Request) -> Response:
dependant=dependant, values=values, is_coroutine=is_coroutine
)

if asyncio.iscoroutine(raw_response):
raw_response = await raw_response

if isinstance(raw_response, Response):
if raw_response.background is None:
raw_response.background = background_tasks
Expand Down
16 changes: 16 additions & 0 deletions tests/test_dependency_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ async def get_callable_dependency(value: str = Depends(callable_dependency)):
return value


app.add_api_route("/callable-dependency-not-decorated", callable_dependency, methods=["GET"])


@app.get("/callable-gen-dependency")
async def get_callable_gen_dependency(value: str = Depends(callable_gen_dependency)):
return value
Expand All @@ -65,6 +68,9 @@ async def get_async_callable_dependency(
return value


app.add_api_route("/async-callable-dependency-not-decorated", async_callable_dependency, methods=["GET"])


@app.get("/async-callable-gen-dependency")
async def get_async_callable_gen_dependency(
value: str = Depends(async_callable_gen_dependency),
Expand All @@ -79,6 +85,9 @@ async def get_synchronous_method_dependency(
return value


app.add_api_route("/synchronous-method-dependency-not-decorated", methods_dependency.synchronous, methods=["GET"])


@app.get("/synchronous-method-gen-dependency")
async def get_synchronous_method_gen_dependency(
value: str = Depends(methods_dependency.synchronous_gen),
Expand All @@ -93,6 +102,9 @@ async def get_asynchronous_method_dependency(
return value


app.add_api_route("/asynchronous-method-dependency-not-decorated", methods_dependency.asynchronous, methods=["GET"])


@app.get("/asynchronous-method-gen-dependency")
async def get_asynchronous_method_gen_dependency(
value: str = Depends(methods_dependency.asynchronous_gen),
Expand All @@ -107,12 +119,16 @@ async def get_asynchronous_method_gen_dependency(
"route,value",
[
("/callable-dependency", "callable-dependency"),
("/callable-dependency-not-decorated", "callable-dependency-not-decorated"),
("/callable-gen-dependency", "callable-gen-dependency"),
("/async-callable-dependency", "async-callable-dependency"),
("/async-callable-dependency-not-decorated", "async-callable-dependency-not-decorated"),
("/async-callable-gen-dependency", "async-callable-gen-dependency"),
("/synchronous-method-dependency", "synchronous-method-dependency"),
("/synchronous-method-dependency-not-decorated", "synchronous-method-dependency-not-decorated"),
("/synchronous-method-gen-dependency", "synchronous-method-gen-dependency"),
("/asynchronous-method-dependency", "asynchronous-method-dependency"),
("/asynchronous-method-dependency-not-decorated", "asynchronous-method-dependency-not-decorated"),
("/asynchronous-method-gen-dependency", "asynchronous-method-gen-dependency"),
],
)
Expand Down

0 comments on commit 5812a5c

Please sign in to comment.