diff --git a/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py b/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py index 1ce6a742125..66908b98ec1 100644 --- a/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py +++ b/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py @@ -440,6 +440,9 @@ def stage_variables(self) -> Optional[Dict[str, str]]: @property def path(self) -> str: + stage = self.request_context.stage + if stage != "$default": + return self.raw_path[len("/" + stage) :] return self.raw_path @property diff --git a/tests/functional/event_handler/test_api_gateway.py b/tests/functional/event_handler/test_api_gateway.py index 3c959747daf..683e1aa6c91 100644 --- a/tests/functional/event_handler/test_api_gateway.py +++ b/tests/functional/event_handler/test_api_gateway.py @@ -823,3 +823,22 @@ def foo(): # THEN a route for `/foo/status` should be found # so no prefix was stripped from the request path assert response["statusCode"] == 200 + + +def test_api_gateway_v2_raw_path(): + # GIVEN a Http API V2 proxy type event + # AND a custom stage name "dev" and raw path "/dev/foo" + app = ApiGatewayResolver(proxy_type=ProxyEventType.APIGatewayProxyEventV2) + event = {"rawPath": "/dev/foo", "requestContext": {"http": {"method": "GET"}, "stage": "dev"}} + + @app.get("/foo") + def foo(): + return {} + + # WHEN calling the event handler + # WITH a route "/foo" + result = app(event, {}) + + # THEN process event correctly + assert result["statusCode"] == 200 + assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON