Skip to content

Commit f31ea17

Browse files
fix(event-handler): handle aliased parameters e.g., Query(alias="categoryType") (#3766)
* fix(event-handler): correctly handle aliased parameters * fix(parameters): make cache aware of single vs multiple calls Signed-off-by: heitorlessa <lessa@amazon.co.uk> * chore: cleanup, add test for single and nested Signed-off-by: heitorlessa <lessa@amazon.co.uk> * chore: no-op exception suppress Signed-off-by: heitorlessa <lessa@amazon.co.uk> * fix: use local event, not global to prevent race condition Signed-off-by: heitorlessa <lessa@amazon.co.uk> * Revert "chore: no-op exception suppress" This reverts commit f79dffc. --------- Signed-off-by: heitorlessa <lessa@amazon.co.uk> Co-authored-by: Heitor Lessa <lessa@amazon.com> Co-authored-by: heitorlessa <lessa@amazon.co.uk>
1 parent e46e32e commit f31ea17

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def _normalize_multi_query_string_with_param(query_string: Optional[Dict[str, st
388388
try:
389389
# if the target parameter is a scalar, we keep the first value of the query string
390390
# regardless if there are more in the payload
391-
query_string[param.name] = query_string[param.name][0]
391+
query_string[param.alias] = query_string[param.alias][0]
392392
except KeyError:
393393
pass
394394
return query_string

tests/functional/event_handler/test_openapi_validation_middleware.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from dataclasses import dataclass
33
from enum import Enum
44
from pathlib import PurePath
5-
from typing import List, Tuple
5+
from typing import List, Optional, Tuple
66

77
import pytest
88
from pydantic import BaseModel
@@ -15,9 +15,11 @@
1515
Response,
1616
VPCLatticeResolver,
1717
VPCLatticeV2Resolver,
18+
content_types,
1819
)
1920
from aws_lambda_powertools.event_handler.openapi.params import Body, Header, Query
2021
from aws_lambda_powertools.shared.types import Annotated
22+
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
2123
from tests.functional.utils import load_event
2224

2325
LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json")
@@ -1018,3 +1020,23 @@ def handler3():
10181020
# IF expected_error_text is provided, THEN check for its presence in the response body
10191021
if expected_error_text:
10201022
assert any(text in result["body"] for text in expected_error_text)
1023+
1024+
1025+
def test_validation_with_alias():
1026+
# GIVEN a Http API V2 proxy type event
1027+
app = APIGatewayRestResolver(enable_validation=True)
1028+
event = load_event("apiGatewayProxyEvent.json")
1029+
1030+
class FunkyTown(BaseModel):
1031+
parameter: str
1032+
1033+
@app.get("/my/path")
1034+
def my_path(
1035+
parameter: Annotated[Optional[str], Query(alias="parameter1")] = None,
1036+
) -> Response[FunkyTown]:
1037+
assert isinstance(app.current_event, APIGatewayProxyEvent)
1038+
assert parameter == "value1"
1039+
return Response(200, content_types.APPLICATION_JSON, FunkyTown(parameter=parameter))
1040+
1041+
result = app(event, {})
1042+
assert result["statusCode"] == 200

0 commit comments

Comments
 (0)