@@ -240,8 +240,8 @@ def invalid_response() -> TodoItem:
240240
241241
242242def test_per_route_validation_with_pydantic_v2 ():
243- """Test that per-route validation works correctly with Pydantic v2 models """
244- # GIVEN APIGatewayRestResolver with mixed validation
243+ """Test that per-route validation actually validates when resolver has validation disabled """
244+ # GIVEN APIGatewayRestResolver WITHOUT global validation
245245 app = APIGatewayRestResolver ()
246246
247247 class Task (BaseModel ):
@@ -250,7 +250,8 @@ class Task(BaseModel):
250250
251251 @app .get ("/task" , enable_validation = True )
252252 def get_task () -> Task :
253- return Task (title = "Important" , priority = 1 )
253+ # Return invalid data — missing 'title' and 'priority'
254+ return cast (Task , {"wrong" : "data" })
254255
255256 @app .get ("/unvalidated-task" )
256257 def get_unvalidated_task ():
@@ -259,13 +260,12 @@ def get_unvalidated_task():
259260 event = load_event ("apiGatewayProxyEvent.json" )
260261 event ["httpMethod" ] = "GET"
261262
262- # WHEN calling validated route
263+ # WHEN calling validated route with invalid data
263264 event ["path" ] = "/task"
264265 result = app (event , {})
265266
266- # THEN should validate and serialize correctly
267- assert result ["statusCode" ] == 200
268- assert "Important" in result ["body" ]
267+ # THEN validation must reject it with 422
268+ assert result ["statusCode" ] == 422
269269
270270 # WHEN calling unvalidated route
271271 event ["path" ] = "/unvalidated-task"
@@ -274,3 +274,28 @@ def get_unvalidated_task():
274274 # THEN should return as-is without validation
275275 assert result ["statusCode" ] == 200
276276 assert "extra" in result ["body" ]
277+
278+
279+ def test_per_route_opt_in_validation_with_valid_data ():
280+ """Test that per-route opt-in validation passes valid data and serializes correctly"""
281+ # GIVEN APIGatewayRestResolver WITHOUT global validation
282+ app = APIGatewayRestResolver ()
283+
284+ class Task (BaseModel ):
285+ title : str
286+ priority : int
287+
288+ @app .get ("/task" , enable_validation = True )
289+ def get_task () -> Task :
290+ return Task (title = "Important" , priority = 1 )
291+
292+ event = load_event ("apiGatewayProxyEvent.json" )
293+ event ["httpMethod" ] = "GET"
294+ event ["path" ] = "/task"
295+
296+ # WHEN calling validated route with valid data
297+ result = app (event , {})
298+
299+ # THEN validation passes and response is serialized
300+ assert result ["statusCode" ] == 200
301+ assert "Important" in result ["body" ]
0 commit comments