@@ -366,6 +366,53 @@ def handler(user: Annotated[Model, Body(embed=True)]) -> Model:
366
366
assert result ["statusCode" ] == 200
367
367
368
368
369
+ def test_validate_body_params_with_missing_body_sets_received_body_none (gw_event ):
370
+ # GIVEN an APIGatewayRestResolver with validation enabled
371
+ app = APIGatewayRestResolver (enable_validation = True )
372
+
373
+ # WHEN a handler is defined with multiple body parameters
374
+ @app .post ("/" )
375
+ def handler (name : str , age : int ):
376
+ return {"name" : name , "age" : age }
377
+
378
+ # WHEN the event has no body
379
+ gw_event ["httpMethod" ] = "POST"
380
+ gw_event ["path" ] = "/"
381
+ gw_event ["body" ] = None # simulate event without body
382
+ gw_event ["headers" ]["content-type" ] = "application/json"
383
+
384
+ result = app (gw_event , {})
385
+
386
+ # THEN the handler should be invoked and return 422
387
+ assert result ["statusCode" ] == 422
388
+ assert "missing" in result ["body" ]
389
+
390
+
391
+ def test_validate_embed_body_param_with_missing_body_sets_received_body_none (gw_event ):
392
+ # GIVEN an APIGatewayRestResolver with validation enabled
393
+ app = APIGatewayRestResolver (enable_validation = True )
394
+
395
+ class Model (BaseModel ):
396
+ name : str
397
+
398
+ # WHEN a handler is defined with a body parameter
399
+ @app .post ("/" )
400
+ def handler (user : Annotated [Model , Body (embed = True )]) -> Model :
401
+ return user
402
+
403
+ # WHEN the event has no body
404
+ gw_event ["httpMethod" ] = "POST"
405
+ gw_event ["path" ] = "/"
406
+ gw_event ["body" ] = None # simulate event without body
407
+ gw_event ["headers" ]["content-type" ] = "application/json"
408
+
409
+ result = app (gw_event , {})
410
+
411
+ # THEN the handler should be invoked and return 422
412
+ assert result ["statusCode" ] == 422
413
+ assert "missing" in result ["body" ]
414
+
415
+
369
416
def test_validate_response_return (gw_event ):
370
417
# GIVEN an APIGatewayRestResolver with validation enabled
371
418
app = APIGatewayRestResolver (enable_validation = True )
0 commit comments