Skip to content

Commit 9775975

Browse files
fix(event_handler): revert regression when validating response (#6234)
Reverting regression
1 parent 76b4d8f commit 9775975

File tree

2 files changed

+2
-76
lines changed

2 files changed

+2
-76
lines changed

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) ->
136136
return self._handle_response(route=route, response=response)
137137

138138
def _handle_response(self, *, route: Route, response: Response):
139-
# Check if we have a return type defined
140-
if route.dependant.return_param:
141-
# Validate and serialize the response, including None
139+
# Process the response body if it exists
140+
if response.body and response.is_json():
142141
response.body = self._serialize_response(
143142
field=route.dependant.return_param,
144143
response_content=response.body,

tests/functional/event_handler/_pydantic/test_openapi_validation_middleware.py

-73
Original file line numberDiff line numberDiff line change
@@ -1128,76 +1128,3 @@ def handler(user_id: int = 123):
11281128
# THEN the handler should be invoked and return 200
11291129
result = app(minimal_event, {})
11301130
assert result["statusCode"] == 200
1131-
1132-
1133-
def test_validation_error_none_returned_non_optional_type(gw_event):
1134-
# GIVEN an APIGatewayRestResolver with validation enabled
1135-
app = APIGatewayRestResolver(enable_validation=True)
1136-
1137-
class Model(BaseModel):
1138-
name: str
1139-
age: int
1140-
1141-
@app.get("/none_not_allowed")
1142-
def handler_none_not_allowed() -> Model:
1143-
return None # type: ignore
1144-
1145-
# WHEN returning None for a non-Optional type
1146-
gw_event["path"] = "/none_not_allowed"
1147-
result = app(gw_event, {})
1148-
1149-
# THEN it should return a validation error
1150-
assert result["statusCode"] == 422
1151-
body = json.loads(result["body"])
1152-
assert "model_attributes_type" in body["detail"][0]["type"]
1153-
1154-
1155-
def test_none_returned_for_optional_type(gw_event):
1156-
# GIVEN an APIGatewayRestResolver with validation enabled
1157-
app = APIGatewayRestResolver(enable_validation=True)
1158-
1159-
class Model(BaseModel):
1160-
name: str
1161-
age: int
1162-
1163-
@app.get("/none_allowed")
1164-
def handler_none_allowed() -> Optional[Model]:
1165-
return None
1166-
1167-
# WHEN returning None for an Optional type
1168-
gw_event["path"] = "/none_allowed"
1169-
result = app(gw_event, {})
1170-
1171-
# THEN it should succeed
1172-
assert result["statusCode"] == 200
1173-
assert result["body"] == "null"
1174-
1175-
1176-
@pytest.mark.parametrize(
1177-
"path, body",
1178-
[
1179-
("/empty_dict", {}),
1180-
("/empty_list", []),
1181-
("/none", "null"),
1182-
("/empty_string", ""),
1183-
],
1184-
ids=["empty_dict", "empty_list", "none", "empty_string"],
1185-
)
1186-
def test_none_returned_for_falsy_return(gw_event, path, body):
1187-
# GIVEN an APIGatewayRestResolver with validation enabled
1188-
app = APIGatewayRestResolver(enable_validation=True)
1189-
1190-
class Model(BaseModel):
1191-
name: str
1192-
age: int
1193-
1194-
@app.get(path)
1195-
def handler_none_allowed() -> Model:
1196-
return body
1197-
1198-
# WHEN returning None for an Optional type
1199-
gw_event["path"] = path
1200-
result = app(gw_event, {})
1201-
1202-
# THEN it should succeed
1203-
assert result["statusCode"] == 422

0 commit comments

Comments
 (0)