Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove version from rest api payload #6532

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 10 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,
sidhujus marked this conversation as resolved.
Show resolved Hide resolved
):
"""
Constructs an ApiGatewayLambdaEvent
Expand Down Expand Up @@ -205,7 +208,9 @@ 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"
# v1 payloads and rest api payloads are identical save for the version field
if api_type == Route.HTTP:
self.version = "1.0"
sidhujus marked this conversation as resolved.
Show resolved Hide resolved
self.http_method = http_method
self.body = body
self.resource = resource
Expand All @@ -218,6 +223,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 +239,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 +255,9 @@ def to_dict(self) -> Dict[str, Any]:
"isBase64Encoded": self.is_base_64_encoded,
}

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
Loading