@@ -366,7 +366,7 @@ 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 ):
369
+ def test_validate_body_param_with_missing_body (gw_event ):
370
370
# GIVEN an APIGatewayRestResolver with validation enabled
371
371
app = APIGatewayRestResolver (enable_validation = True )
372
372
@@ -388,7 +388,29 @@ def handler(name: str, age: int):
388
388
assert "missing" in result ["body" ]
389
389
390
390
391
- def test_validate_embed_body_param_with_missing_body_sets_received_body_none (gw_event ):
391
+ def test_validate_body_param_with_empty_body (gw_event ):
392
+ # GIVEN an APIGatewayRestResolver with validation enabled
393
+ app = APIGatewayRestResolver (enable_validation = True )
394
+
395
+ # WHEN a handler is defined with multiple body parameters
396
+ @app .post ("/" )
397
+ def handler (name : str , age : int ):
398
+ return {"name" : name , "age" : age }
399
+
400
+ # WHEN the event has no body
401
+ gw_event ["httpMethod" ] = "POST"
402
+ gw_event ["path" ] = "/"
403
+ gw_event ["body" ] = "[]" # JSON array -> received_body is a list (no .get)
404
+ gw_event ["headers" ]["content-type" ] = "application/json"
405
+
406
+ result = app (gw_event , {})
407
+
408
+ # THEN the handler should be invoked and return 422
409
+ assert result ["statusCode" ] == 422
410
+ assert "missing" in result ["body" ]
411
+
412
+
413
+ def test_validate_embed_body_param_with_missing_body (gw_event ):
392
414
# GIVEN an APIGatewayRestResolver with validation enabled
393
415
app = APIGatewayRestResolver (enable_validation = True )
394
416
@@ -413,6 +435,31 @@ def handler(user: Annotated[Model, Body(embed=True)]) -> Model:
413
435
assert "missing" in result ["body" ]
414
436
415
437
438
+ def test_validate_embed_body_param_with_empty_body (gw_event ):
439
+ # GIVEN an APIGatewayRestResolver with validation enabled
440
+ app = APIGatewayRestResolver (enable_validation = True )
441
+
442
+ class Model (BaseModel ):
443
+ name : str
444
+
445
+ # WHEN a handler is defined with a body parameter
446
+ @app .post ("/" )
447
+ def handler (user : Annotated [Model , Body (embed = True )]) -> Model :
448
+ return user
449
+
450
+ # WHEN the event has no body
451
+ gw_event ["httpMethod" ] = "POST"
452
+ gw_event ["path" ] = "/"
453
+ gw_event ["body" ] = "[]" # JSON array -> received_body is a list (no .get)
454
+ gw_event ["headers" ]["content-type" ] = "application/json"
455
+
456
+ result = app (gw_event , {})
457
+
458
+ # THEN the handler should be invoked and return 422
459
+ assert result ["statusCode" ] == 422
460
+ assert "missing" in result ["body" ]
461
+
462
+
416
463
def test_validate_response_return (gw_event ):
417
464
# GIVEN an APIGatewayRestResolver with validation enabled
418
465
app = APIGatewayRestResolver (enable_validation = True )
0 commit comments