Skip to content

fix(event-handler): handle aliased parameters e.g., Query(alias="categoryType") #3766

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

Merged
merged 8 commits into from
Feb 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def _normalize_multi_query_string_with_param(query_string: Optional[Dict[str, st
try:
# if the target parameter is a scalar, we keep the first value of the query string
# regardless if there are more in the payload
query_string[param.name] = query_string[param.name][0]
query_string[param.alias] = query_string[param.alias][0]
except KeyError:
pass
return query_string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dataclasses import dataclass
from enum import Enum
from pathlib import PurePath
from typing import List, Tuple
from typing import List, Optional, Tuple

import pytest
from pydantic import BaseModel
Expand All @@ -15,9 +15,11 @@
Response,
VPCLatticeResolver,
VPCLatticeV2Resolver,
content_types,
)
from aws_lambda_powertools.event_handler.openapi.params import Body, Header, Query
from aws_lambda_powertools.shared.types import Annotated
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
from tests.functional.utils import load_event

LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json")
Expand Down Expand Up @@ -1018,3 +1020,23 @@ def handler3():
# IF expected_error_text is provided, THEN check for its presence in the response body
if expected_error_text:
assert any(text in result["body"] for text in expected_error_text)


def test_validation_with_alias():
# GIVEN a Http API V2 proxy type event
app = APIGatewayRestResolver(enable_validation=True)
event = load_event("apiGatewayProxyEvent.json")

class FunkyTown(BaseModel):
parameter: str

@app.get("/my/path")
def my_path(
parameter: Annotated[Optional[str], Query(alias="parameter1")] = None,
) -> Response[FunkyTown]:
assert isinstance(app.current_event, APIGatewayProxyEvent)
assert parameter == "value1"
return Response(200, content_types.APPLICATION_JSON, FunkyTown(parameter=parameter))

result = app(event, {})
assert result["statusCode"] == 200