From 0ae9ba811aa0c5dd69b3d1a1edf3693be244e4a5 Mon Sep 17 00:00:00 2001 From: Ruwann Date: Fri, 29 Dec 2023 14:52:05 +0100 Subject: [PATCH 1/2] Bugfix/starlette root path (#1833) Fixes the change in behaviour in starlette v0.33 via PR [#2352](https://github.com/encode/starlette/pull/2352) --- connexion/middleware/routing.py | 7 ++++--- connexion/middleware/swagger_ui.py | 4 +++- pyproject.toml | 2 +- tests/test_flask_encoder.py | 6 +++--- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/connexion/middleware/routing.py b/connexion/middleware/routing.py index 7f4be932f..cac632d40 100644 --- a/connexion/middleware/routing.py +++ b/connexion/middleware/routing.py @@ -35,9 +35,10 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: scope.get("path_params", {}) ) - api_base_path = scope.get("root_path", "")[ - len(original_scope.get("root_path", "")) : - ] + def get_root_path(scope: Scope) -> str: + return scope.get("route_root_path", scope.get("root_path", "")) + + api_base_path = get_root_path(scope)[len(get_root_path(original_scope)) :] extensions = original_scope.setdefault("extensions", {}) connexion_routing = extensions.setdefault(ROUTING_CONTEXT, {}) diff --git a/connexion/middleware/swagger_ui.py b/connexion/middleware/swagger_ui.py index 90d67fa45..a9bded0fe 100644 --- a/connexion/middleware/swagger_ui.py +++ b/connexion/middleware/swagger_ui.py @@ -59,7 +59,9 @@ def _base_path_for_prefix(self, request: StarletteRequest) -> str: """ returns a modified basePath which includes the incoming root_path. """ - return request.scope.get("root_path", "").rstrip("/") + return request.scope.get( + "route_root_path", request.scope.get("root_path", "") + ).rstrip("/") def _spec_for_prefix(self, request): """ diff --git a/pyproject.toml b/pyproject.toml index 5fed8b47d..2ac6c6057 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,7 @@ Jinja2 = ">= 3.0.0" python-multipart = ">= 0.0.5" PyYAML = ">= 5.1" requests = ">= 2.27" -starlette = ">= 0.27, <0.33" +starlette = ">= 0.27" typing-extensions = ">= 4" werkzeug = ">= 2.2.1" diff --git a/tests/test_flask_encoder.py b/tests/test_flask_encoder.py index f0702427b..6b11caf56 100644 --- a/tests/test_flask_encoder.py +++ b/tests/test_flask_encoder.py @@ -75,16 +75,16 @@ def get_value(data, path): assert example == "a7b8869c-5f24-4ce0-a5d1-3e44c3663aa9" res = app_client.get("/v1.0/datetime") - assert res.status_code == 200, f"Error is {res.data}" + assert res.status_code == 200, f"Error is {res.text}" data = res.json() assert data == {"value": "2000-01-02T03:04:05.000006Z"} res = app_client.get("/v1.0/date") - assert res.status_code == 200, f"Error is {res.data}" + assert res.status_code == 200, f"Error is {res.text}" data = res.json() assert data == {"value": "2000-01-02"} res = app_client.get("/v1.0/uuid") - assert res.status_code == 200, f"Error is {res.data}" + assert res.status_code == 200, f"Error is {res.text}" data = res.json() assert data == {"value": "e7ff66d0-3ec2-4c4e-bed0-6e4723c24c51"} From f8f461c5240f6d01b0d8c7104387993ce811b2c7 Mon Sep 17 00:00:00 2001 From: Robert Parini Date: Tue, 2 Jan 2024 11:14:19 +0000 Subject: [PATCH 2/2] Fix quickstart WSGI example typo (#1845) Small fix to the example code in the documentation for using the `a2wsgi` workaround. The `app` variable is not defined prior to `app = ConnexionMiddleware(app)` --- docs/quickstart.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index d2893f259..cd9c9bfee 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -102,7 +102,7 @@ built using either the :code:`AsyncApp` or :code:`FlaskApp`. wsgi_app = App(__name__) asgi_app = WSGIMiddleware(wsgi_app) - app = ConnexionMiddleware(app) + app = ConnexionMiddleware(asgi_app) .. dropdown:: View a detailed reference of the options accepted by the :code:`ConnexionMiddleware`