Skip to content

Commit c8a88d5

Browse files
test(event_handler): cover optional and model sequence headers, drop unreachable list-wrapping branches
1 parent 91470b2 commit c8a88d5

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎aws_lambda_powertools/event_handler/middlewares/openapi_validation.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,9 @@ def _process_sequence_param(
679679
except KeyError:
680680
return
681681

682-
if value is not None and not isinstance(value, list):
683-
if isinstance(value, str):
684-
input_dict[param.alias] = value.split(",")
685-
else:
686-
input_dict[param.alias] = [value]
682+
# headers arrive as a single string (possibly comma-joined), query strings already as a list
683+
if isinstance(value, str):
684+
input_dict[param.alias] = value.split(",")
687685

688686

689687
def _process_model_param(
@@ -705,8 +703,6 @@ def _process_model_param(
705703
value = _restore_scalar_parameter(value, source_name, multi_value_params)
706704
elif isinstance(value, str):
707705
value = value.split(",")
708-
elif not isinstance(value, list):
709-
value = [value]
710706
model_data[field_alias] = _normalize_field_value(value=value, field_info=field_info)
711707

712708
input_dict[param.alias] = model_data

‎tests/functional/event_handler/_pydantic/test_openapi_validation_middleware.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,3 +4456,65 @@ def create(item: Item):
44564456
result = app(gw_event, {})
44574457
assert result["statusCode"] == 200
44584458
assert json.loads(result["body"]) == {"name": "powertools"}
4459+
4460+
4461+
def test_header_sequence_optional_absent_uses_default(gw_event_http):
4462+
# GIVEN an APIGatewayHttpResolver with validation enabled and an optional sequence header that is not sent
4463+
app = APIGatewayHttpResolver(enable_validation=True)
4464+
gw_event_http["rawPath"] = gw_event_http["requestContext"]["http"]["path"] = "/hello"
4465+
gw_event_http["requestContext"]["http"]["method"] = "GET"
4466+
gw_event_http["headers"] = {}
4467+
4468+
@app.get("/hello")
4469+
def hello(x_country: Annotated[list[str] | None, Header(alias="x-country")] = None):
4470+
return {"c": x_country}
4471+
4472+
# WHEN the request has no such header
4473+
result = app(gw_event_http, {})
4474+
4475+
# THEN the default is used instead of failing validation
4476+
assert result["statusCode"] == 200
4477+
assert json.loads(result["body"]) == {"c": None}
4478+
4479+
4480+
def test_header_model_sequence_field_single_value(gw_event_http):
4481+
# GIVEN a Pydantic header model with a sequence field and a single header value on HTTP API
4482+
class HeaderModel(BaseModel):
4483+
x_country: list[str] = Field(alias="x-country")
4484+
x_trace: str = Field(alias="x-trace")
4485+
4486+
app = APIGatewayHttpResolver(enable_validation=True)
4487+
gw_event_http["rawPath"] = gw_event_http["requestContext"]["http"]["path"] = "/hello"
4488+
gw_event_http["requestContext"]["http"]["method"] = "GET"
4489+
gw_event_http["headers"] = {"x-country": "US", "x-trace": "t-1"}
4490+
4491+
@app.get("/hello")
4492+
def hello(headers: Annotated[HeaderModel, Header()]):
4493+
return {"c": headers.x_country, "t": headers.x_trace}
4494+
4495+
# WHEN the request carries one value for the sequence field
4496+
result = app(gw_event_http, {})
4497+
4498+
# THEN it is accepted as a one-item list and the scalar field is untouched
4499+
assert result["statusCode"] == 200
4500+
assert json.loads(result["body"]) == {"c": ["US"], "t": "t-1"}
4501+
4502+
4503+
def test_header_model_sequence_field_comma_joined(gw_event_http):
4504+
# GIVEN a Pydantic header model with a sequence field and a comma-joined header value
4505+
class HeaderModel(BaseModel):
4506+
x_country: list[str] = Field(alias="x-country")
4507+
4508+
app = APIGatewayHttpResolver(enable_validation=True)
4509+
gw_event_http["rawPath"] = gw_event_http["requestContext"]["http"]["path"] = "/hello"
4510+
gw_event_http["requestContext"]["http"]["method"] = "GET"
4511+
gw_event_http["headers"] = {"x-country": "US,PT"}
4512+
4513+
@app.get("/hello")
4514+
def hello(headers: Annotated[HeaderModel, Header()]):
4515+
return {"c": headers.x_country}
4516+
4517+
result = app(gw_event_http, {})
4518+
4519+
assert result["statusCode"] == 200
4520+
assert json.loads(result["body"]) == {"c": ["US", "PT"]}

0 commit comments

Comments
 (0)