From cfa435b87fadc6e02f1a60b4bd5d2a5c4cfd6d77 Mon Sep 17 00:00:00 2001 From: sidhujus <105385029+sidhujus@users.noreply.github.com> Date: Tue, 9 Jan 2024 09:15:20 -0800 Subject: [PATCH] fix: remove version from rest api payload (#6532) * remove version from rest api payload * remove unused variable * update documentation * fix format error --- samcli/local/apigw/event_constructor.py | 8 +++++-- samcli/local/apigw/local_apigw_service.py | 1 + samcli/local/events/api_event.py | 11 ++++++++-- .../local/apigw/test_local_apigw_service.py | 2 ++ tests/unit/local/events/test_api_event.py | 22 ++++++++++++++++++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/samcli/local/apigw/event_constructor.py b/samcli/local/apigw/event_constructor.py index 441d94d20e..b9e07ecb8d 100644 --- a/samcli/local/apigw/event_constructor.py +++ b/samcli/local/apigw/event_constructor.py @@ -9,6 +9,7 @@ from typing import Any, Dict from samcli.local.apigw.path_converter import PathConverter +from samcli.local.apigw.route import Route from samcli.local.events.api_event import ( ApiGatewayLambdaEvent, ApiGatewayV2LambdaEvent, @@ -22,16 +23,18 @@ def construct_v1_event( - flask_request, port, binary_types, stage_name=None, stage_variables=None, operation_name=None + flask_request, port, binary_types, stage_name=None, stage_variables=None, operation_name=None, api_type=Route.API ) -> Dict[str, Any]: """ - Helper method that constructs the Event to be passed to Lambda + Helper method that constructs the Event to be passed to Lambda. + Used for Http apis with payload v1 and Rest apis because the payloads are almost identical :param request flask_request: Flask Request :param port: the port number :param binary_types: list of binary types :param stage_name: Optional, the stage name string :param stage_variables: Optional, API Gateway Stage Variables + :param api_type: Optional, the type of api payload being constructed :return: JSON object """ @@ -86,6 +89,7 @@ def construct_v1_event( path=flask_request.path, is_base_64_encoded=is_base_64, stage_variables=stage_variables, + api_type=api_type, ) event_dict = event.to_dict() diff --git a/samcli/local/apigw/local_apigw_service.py b/samcli/local/apigw/local_apigw_service.py index f5a0ea0802..1e0f871fcd 100644 --- a/samcli/local/apigw/local_apigw_service.py +++ b/samcli/local/apigw/local_apigw_service.py @@ -477,6 +477,7 @@ def _generate_lambda_event(self, flask_request: Request, route: Route, method: s stage_name=self.api.stage_name, stage_variables=self.api.stage_variables, operation_name=route_key, + api_type=route.event_type, ) def _build_v1_context(self, route: Route) -> Dict[str, Any]: diff --git a/samcli/local/events/api_event.py b/samcli/local/events/api_event.py index 1b82c7caea..f73e554d9a 100644 --- a/samcli/local/events/api_event.py +++ b/samcli/local/events/api_event.py @@ -4,6 +4,8 @@ from time import time from typing import Any, Dict +from samcli.local.apigw.route import Route + class ContextIdentity: def __init__( @@ -169,6 +171,7 @@ def __init__( stage_variables=None, path=None, is_base_64_encoded=False, + api_type=Route.API, ): """ Constructs an ApiGatewayLambdaEvent @@ -185,6 +188,7 @@ def __init__( :param dict stage_variables: API Gateway Stage Variables :param str path: Path of the request :param bool is_base_64_encoded: True if the data is base64 encoded. + :param str api_type: The type of API the event is being generated for """ if not isinstance(query_string_params, dict) and query_string_params is not None: @@ -205,7 +209,6 @@ def __init__( if not isinstance(stage_variables, dict) and stage_variables is not None: raise TypeError("'stage_variables' must be of type dict or None") - self.version = "1.0" self.http_method = http_method self.body = body self.resource = resource @@ -218,6 +221,7 @@ def __init__( self.stage_variables = stage_variables self.path = path self.is_base_64_encoded = is_base_64_encoded + self.api_type = api_type def to_dict(self) -> Dict[str, Any]: """ @@ -233,7 +237,6 @@ def to_dict(self) -> Dict[str, Any]: request_context_dict = self.request_context.to_dict() json_dict = { - "version": self.version, "httpMethod": self.http_method, "body": self.body if self.body else None, "resource": self.resource, @@ -250,6 +253,10 @@ def to_dict(self) -> Dict[str, Any]: "isBase64Encoded": self.is_base_64_encoded, } + # v1 payloads and rest api payloads are identical save for the version field + if self.api_type == Route.HTTP: + json_dict["version"] = "1.0" + return json_dict diff --git a/tests/unit/local/apigw/test_local_apigw_service.py b/tests/unit/local/apigw/test_local_apigw_service.py index e77ce996ac..c3442aaf06 100644 --- a/tests/unit/local/apigw/test_local_apigw_service.py +++ b/tests/unit/local/apigw/test_local_apigw_service.py @@ -123,6 +123,7 @@ def test_api_request_must_invoke_lambda(self, v2_event_mock, v1_event_mock, requ stage_name=ANY, stage_variables=ANY, operation_name="getRestApi", + api_type=Route.API, ) @patch.object(LocalApigwService, "get_request_methods_endpoints") @@ -190,6 +191,7 @@ def test_http_v1_payload_request_must_invoke_lambda(self, v2_event_mock, v1_even stage_name=ANY, stage_variables=ANY, operation_name=None, + api_type=Route.HTTP, ) @patch.object(LocalApigwService, "get_request_methods_endpoints") diff --git a/tests/unit/local/events/test_api_event.py b/tests/unit/local/events/test_api_event.py index e4e3d26e65..6cd3e6c3b0 100644 --- a/tests/unit/local/events/test_api_event.py +++ b/tests/unit/local/events/test_api_event.py @@ -3,6 +3,7 @@ from time import time from datetime import datetime +from samcli.local.apigw.route import Route from samcli.local.events.api_event import ( ContextIdentity, @@ -335,7 +336,6 @@ def test_to_dict(self): ) expected = { - "version": "1.0", "httpMethod": "request_method", "body": "request_data", "resource": "resource", @@ -355,6 +355,26 @@ def test_to_dict(self): def test_to_dict_with_defaults(self): event = ApiGatewayLambdaEvent() + expected = { + "httpMethod": None, + "body": None, + "resource": None, + "requestContext": {}, + "queryStringParameters": None, + "multiValueQueryStringParameters": None, + "headers": None, + "multiValueHeaders": None, + "pathParameters": None, + "stageVariables": None, + "path": None, + "isBase64Encoded": False, + } + + self.assertEqual(event.to_dict(), expected) + + def test_to_dict_with_http_v1(self): + event = ApiGatewayLambdaEvent(api_type=Route.HTTP) + expected = { "version": "1.0", "httpMethod": None,