Skip to content

Commit 2e60f63

Browse files
fix(event_handler): handle ALB response when it's None
1 parent b1e9eed commit 2e60f63

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def _handle_response(self, *, route: Route, response: Response):
298298
else:
299299
# ALB resolver converts None body to "" to prevent ALB 5xx errors,
300300
# but the validation should still see it as None.
301-
response_content = None if response.body == "" and field.type_ is None else response.body
301+
response_content = None if response.body == "" and field.type_ in (None, type(None)) else response.body
302302

303303
response.body = self._serialize_response_with_validation(
304304
field=field,

tests/functional/event_handler/_pydantic/test_openapi_validation_middleware.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,3 +4245,21 @@ def handler() -> None:
42454245
result = app(gw_event_alb, {})
42464246
assert result["statusCode"] == 204
42474247
assert result["body"] == ""
4248+
4249+
4250+
def test_alb_response_typed_none_body_with_validation(gw_event_alb):
4251+
# GIVEN an ALBResolver with validation enabled
4252+
app = ALBResolver(enable_validation=True)
4253+
4254+
gw_event_alb["path"] = "/no-content"
4255+
gw_event_alb["httpMethod"] = "DELETE"
4256+
4257+
# WHEN a handler returns Response[None] with body=None
4258+
@app.delete("/no-content")
4259+
def handler() -> Response[None]:
4260+
return Response(status_code=204, body=None)
4261+
4262+
# THEN the response should be 204 with empty body (not 422 validation error)
4263+
result = app(gw_event_alb, {})
4264+
assert result["statusCode"] == 204
4265+
assert result["body"] == ""

0 commit comments

Comments
 (0)