Skip to content

Commit 99659bf

Browse files
committed
fix: improve coverage
1 parent 537a481 commit 99659bf

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,10 @@ def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) ->
110110
def _handle_response(self, *, route: Route, response: Response):
111111
# Process the response body if it exists
112112
if response.body:
113-
raw_response = jsonable_encoder(response.body)
114-
115113
# Validate and serialize the response, if it's JSON
116114
if response.is_json():
117115
response.body = json.dumps(
118-
self._serialize_response(field=route.dependant.return_param, response_content=raw_response),
116+
self._serialize_response(field=route.dependant.return_param, response_content=response.body),
119117
sort_keys=True,
120118
)
121119

tests/functional/event_handler/test_openapi_validation_middleware.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from dataclasses import dataclass
33
from enum import Enum
44
from pathlib import PurePath
5-
from typing import Tuple
5+
from typing import List, Tuple
66

77
import pytest
88
from pydantic import BaseModel
@@ -113,6 +113,24 @@ def handler() -> int:
113113
assert result["body"] == "123"
114114

115115

116+
def test_validate_return_list():
117+
# GIVEN an APIGatewayRestResolver with validation enabled
118+
app = APIGatewayRestResolver(enable_validation=True)
119+
120+
# WHEN a handler is defined with a return type
121+
@app.get("/")
122+
def handler() -> List[int]:
123+
return [123, 234]
124+
125+
LOAD_GW_EVENT["path"] = "/"
126+
127+
# THEN the handler should be invoked and return 200
128+
# THEN the body must be [123, 234]
129+
result = app(LOAD_GW_EVENT, {})
130+
assert result["statusCode"] == 200
131+
assert result["body"] == "[123, 234]"
132+
133+
116134
def test_validate_return_tuple():
117135
# GIVEN an APIGatewayRestResolver with validation enabled
118136
app = APIGatewayRestResolver(enable_validation=True)
@@ -143,7 +161,7 @@ def test_validate_return_purepath():
143161
# WHEN return value is a PurePath
144162
@app.get("/")
145163
def handler() -> str:
146-
return sample_path
164+
return sample_path.as_posix()
147165

148166
LOAD_GW_EVENT["path"] = "/"
149167

@@ -266,6 +284,30 @@ def handler(user: Model) -> Model:
266284
assert json.loads(result["body"]) == {"name": "John", "age": 30}
267285

268286

287+
def test_validate_body_param_with_invalid_date():
288+
# GIVEN an APIGatewayRestResolver with validation enabled
289+
app = APIGatewayRestResolver(enable_validation=True)
290+
291+
class Model(BaseModel):
292+
name: str
293+
age: int
294+
295+
# WHEN a handler is defined with a body parameter
296+
@app.post("/")
297+
def handler(user: Model) -> Model:
298+
return user
299+
300+
LOAD_GW_EVENT["httpMethod"] = "POST"
301+
LOAD_GW_EVENT["path"] = "/"
302+
LOAD_GW_EVENT["body"] = "{" # invalid JSON
303+
304+
# THEN the handler should be invoked and return 422
305+
# THEN the body must have the "json_invalid" error message
306+
result = app(LOAD_GW_EVENT, {})
307+
assert result["statusCode"] == 422
308+
assert "json_invalid" in result["body"]
309+
310+
269311
def test_validate_embed_body_param():
270312
# GIVEN an APIGatewayRestResolver with validation enabled
271313
app = APIGatewayRestResolver(enable_validation=True)

0 commit comments

Comments
 (0)