Skip to content

fix(event_handler): revert regression when validating response #6234

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 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) ->
return self._handle_response(route=route, response=response)

def _handle_response(self, *, route: Route, response: Response):
# Check if we have a return type defined
if route.dependant.return_param:
# Validate and serialize the response, including None
# Process the response body if it exists
if response.body and response.is_json():
response.body = self._serialize_response(
field=route.dependant.return_param,
response_content=response.body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,76 +1128,3 @@ def handler(user_id: int = 123):
# THEN the handler should be invoked and return 200
result = app(minimal_event, {})
assert result["statusCode"] == 200


def test_validation_error_none_returned_non_optional_type(gw_event):
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)

class Model(BaseModel):
name: str
age: int

@app.get("/none_not_allowed")
def handler_none_not_allowed() -> Model:
return None # type: ignore

# WHEN returning None for a non-Optional type
gw_event["path"] = "/none_not_allowed"
result = app(gw_event, {})

# THEN it should return a validation error
assert result["statusCode"] == 422
body = json.loads(result["body"])
assert "model_attributes_type" in body["detail"][0]["type"]


def test_none_returned_for_optional_type(gw_event):
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)

class Model(BaseModel):
name: str
age: int

@app.get("/none_allowed")
def handler_none_allowed() -> Optional[Model]:
return None

# WHEN returning None for an Optional type
gw_event["path"] = "/none_allowed"
result = app(gw_event, {})

# THEN it should succeed
assert result["statusCode"] == 200
assert result["body"] == "null"


@pytest.mark.parametrize(
"path, body",
[
("/empty_dict", {}),
("/empty_list", []),
("/none", "null"),
("/empty_string", ""),
],
ids=["empty_dict", "empty_list", "none", "empty_string"],
)
def test_none_returned_for_falsy_return(gw_event, path, body):
# GIVEN an APIGatewayRestResolver with validation enabled
app = APIGatewayRestResolver(enable_validation=True)

class Model(BaseModel):
name: str
age: int

@app.get(path)
def handler_none_allowed() -> Model:
return body

# WHEN returning None for an Optional type
gw_event["path"] = path
result = app(gw_event, {})

# THEN it should succeed
assert result["statusCode"] == 422
Loading