Skip to content

Commit

Permalink
fix: remove version from rest api payload (#6532)
Browse files Browse the repository at this point in the history
* remove version from rest api payload

* remove unused variable

* update documentation

* fix format error
  • Loading branch information
sidhujus authored Jan 9, 2024
1 parent db5d131 commit cfa435b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
8 changes: 6 additions & 2 deletions samcli/local/apigw/event_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
"""

Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions samcli/local/apigw/local_apigw_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
11 changes: 9 additions & 2 deletions samcli/local/events/api_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from time import time
from typing import Any, Dict

from samcli.local.apigw.route import Route


class ContextIdentity:
def __init__(
Expand Down Expand Up @@ -169,6 +171,7 @@ def __init__(
stage_variables=None,
path=None,
is_base_64_encoded=False,
api_type=Route.API,
):
"""
Constructs an ApiGatewayLambdaEvent
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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]:
"""
Expand All @@ -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,
Expand All @@ -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


Expand Down
2 changes: 2 additions & 0 deletions tests/unit/local/apigw/test_local_apigw_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/local/events/test_api_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -335,7 +336,6 @@ def test_to_dict(self):
)

expected = {
"version": "1.0",
"httpMethod": "request_method",
"body": "request_data",
"resource": "resource",
Expand All @@ -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,
Expand Down

0 comments on commit cfa435b

Please sign in to comment.