@@ -1436,3 +1436,59 @@ def get_foo(
14361436 body = json .loads (result ["body" ])
14371437 assert body ["int_query" ] == 20
14381438 assert body ["str_query" ] == "fooBarFizzBuzz"
1439+
1440+
1441+ def test_body_alias_sets_validation_alias_automatically ():
1442+ """
1443+ When alias is set but validation_alias is not in Body,
1444+ validation_alias should be automatically set to alias value.
1445+ """
1446+ # GIVEN an APIGatewayRestResolver with validation enabled
1447+ app = APIGatewayRestResolver (enable_validation = True )
1448+
1449+ @app .post ("/foo" )
1450+ def post_foo (
1451+ my_body : Annotated [str , Body (alias = "myBody" )],
1452+ ):
1453+ return {"my_body" : my_body }
1454+
1455+ # WHEN sending a request with body
1456+ event = {
1457+ "httpMethod" : "POST" ,
1458+ "path" : "/foo" ,
1459+ "body" : '"test_value"' ,
1460+ }
1461+
1462+ # THEN the request should succeed
1463+ result = app (event , {})
1464+ assert result ["statusCode" ] == 200
1465+ body = json .loads (result ["body" ])
1466+ assert body ["my_body" ] == "test_value"
1467+
1468+
1469+ def test_body_validation_alias_only_sets_alias_automatically ():
1470+ """
1471+ When only validation_alias is set (without alias) in Body,
1472+ alias should be automatically set to validation_alias value.
1473+ """
1474+ # GIVEN an APIGatewayRestResolver with validation enabled
1475+ app = APIGatewayRestResolver (enable_validation = True )
1476+
1477+ @app .post ("/foo" )
1478+ def post_foo (
1479+ my_body : Annotated [str , Body (validation_alias = "myBody" )],
1480+ ):
1481+ return {"my_body" : my_body }
1482+
1483+ # WHEN sending a request with body
1484+ event = {
1485+ "httpMethod" : "POST" ,
1486+ "path" : "/foo" ,
1487+ "body" : '"test_value"' ,
1488+ }
1489+
1490+ # THEN the request should succeed
1491+ result = app (event , {})
1492+ assert result ["statusCode" ] == 200
1493+ body = json .loads (result ["body" ])
1494+ assert body ["my_body" ] == "test_value"
0 commit comments