From 70dddb1703e8036355349813cff9446936f7cb49 Mon Sep 17 00:00:00 2001 From: sidux Date: Fri, 29 Dec 2023 00:33:31 +0100 Subject: [PATCH 1/3] fix generator handling --- connexion/decorators/response.py | 2 ++ connexion/frameworks/starlette.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/connexion/decorators/response.py b/connexion/decorators/response.py index 160ace61c..db0767802 100644 --- a/connexion/decorators/response.py +++ b/connexion/decorators/response.py @@ -189,6 +189,8 @@ async def wrapper(*args, **kwargs): object. """ handler_response = await function(*args, **kwargs) + if isinstance(handler_response, t.Coroutine): + handler_response = await handler_response if self.framework.is_framework_response(handler_response): return handler_response elif isinstance(handler_response, ConnexionResponse): diff --git a/connexion/frameworks/starlette.py b/connexion/frameworks/starlette.py index 097c10e54..7449c1df8 100644 --- a/connexion/frameworks/starlette.py +++ b/connexion/frameworks/starlette.py @@ -5,6 +5,7 @@ import starlette.convertors from starlette.responses import JSONResponse as StarletteJSONResponse from starlette.responses import Response as StarletteResponse +from starlette.responses import StreamingResponse as StarletteStreamingResponse from starlette.types import Receive, Scope from connexion.frameworks.abstract import Framework @@ -37,6 +38,8 @@ def build_response( ): if isinstance(data, dict) or isinstance(data, list): response_cls = StarletteJSONResponse + elif isinstance(data, typing.Generator): + response_cls = StarletteStreamingResponse else: response_cls = StarletteResponse From bc04aa502c3d94e9af0095ce7e84bfa15ed6d4e3 Mon Sep 17 00:00:00 2001 From: sidux Date: Fri, 29 Dec 2023 00:38:08 +0100 Subject: [PATCH 2/3] fix import --- connexion/frameworks/starlette.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connexion/frameworks/starlette.py b/connexion/frameworks/starlette.py index 7449c1df8..1df69d596 100644 --- a/connexion/frameworks/starlette.py +++ b/connexion/frameworks/starlette.py @@ -38,7 +38,7 @@ def build_response( ): if isinstance(data, dict) or isinstance(data, list): response_cls = StarletteJSONResponse - elif isinstance(data, typing.Generator): + elif isinstance(data, t.Generator): response_cls = StarletteStreamingResponse else: response_cls = StarletteResponse From 85ee1479eb67859b2f2a971a348f12244de9b8bf Mon Sep 17 00:00:00 2001 From: sidux Date: Fri, 29 Dec 2023 00:47:44 +0100 Subject: [PATCH 3/3] handle async generator in starlette --- connexion/frameworks/starlette.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connexion/frameworks/starlette.py b/connexion/frameworks/starlette.py index 1df69d596..44e8503e6 100644 --- a/connexion/frameworks/starlette.py +++ b/connexion/frameworks/starlette.py @@ -38,7 +38,7 @@ def build_response( ): if isinstance(data, dict) or isinstance(data, list): response_cls = StarletteJSONResponse - elif isinstance(data, t.Generator): + elif isinstance(data, t.Generator) or isinstance(data, t.AsyncGenerator): response_cls = StarletteStreamingResponse else: response_cls = StarletteResponse