mirror of https://github.com/tiangolo/fastapi.git
Fix call async routes decorated with class based decorator
This commit is contained in:
parent
7ef0b08897
commit
0b0b4fe4d5
|
|
@ -208,7 +208,12 @@ def get_request_handler(
|
|||
dependency_overrides_provider: Optional[Any] = None,
|
||||
) -> Callable[[Request], Coroutine[Any, Any, Response]]:
|
||||
assert dependant.call is not None, "dependant.call must be a function"
|
||||
is_coroutine = asyncio.iscoroutinefunction(dependant.call)
|
||||
if inspect.isfunction(dependant.call):
|
||||
is_coroutine = asyncio.iscoroutinefunction(dependant.call)
|
||||
else:
|
||||
is_coroutine = asyncio.iscoroutinefunction(
|
||||
getattr(dependant.call, "__call__", dependant.call) # noqa: B004
|
||||
)
|
||||
is_body_form = body_field and isinstance(body_field.field_info, params.Form)
|
||||
if isinstance(response_class, DefaultPlaceholder):
|
||||
actual_response_class: Type[Response] = response_class.value
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
from functools import update_wrapper
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
class SomeDecorator:
|
||||
def __init__(self, original_route):
|
||||
update_wrapper(wrapper=self, wrapped=original_route)
|
||||
self.route = original_route
|
||||
|
||||
async def __call__(self, *args, **kwargs):
|
||||
return await self.route(*args, **kwargs)
|
||||
|
||||
|
||||
data = {"working": True}
|
||||
|
||||
|
||||
@SomeDecorator
|
||||
async def route():
|
||||
return data
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
app.get("/")(route)
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_endpoint_with_async_class_decorator():
|
||||
response = client.get("/")
|
||||
assert data == response.json()
|
||||
Loading…
Reference in New Issue