@@ -342,21 +342,39 @@ class Model(BaseModel):
342
342
343
343
# WHEN a handler is defined with a body parameter
344
344
@app .post ("/" )
345
- def handler (user : Annotated [ Model , Body ( embed = True )] ) -> Response [Model ]:
345
+ def handler (user : Model ) -> Response [Model ]:
346
346
return Response (body = user , status_code = 200 )
347
347
348
348
LOAD_GW_EVENT ["httpMethod" ] = "POST"
349
349
LOAD_GW_EVENT ["path" ] = "/"
350
350
LOAD_GW_EVENT ["body" ] = json .dumps ({"name" : "John" , "age" : 30 })
351
351
352
- # THEN the handler should be invoked and return 422
353
- # THEN the body must be a dict
354
- result = app (LOAD_GW_EVENT , {})
355
- assert result ["statusCode" ] == 422
356
- assert "missing" in result ["body" ]
357
-
358
352
# THEN the handler should be invoked and return 200
359
353
# THEN the body must be a dict
360
- LOAD_GW_EVENT ["body" ] = json .dumps ({"user" : {"name" : "John" , "age" : 30 }})
361
354
result = app (LOAD_GW_EVENT , {})
362
355
assert result ["statusCode" ] == 200
356
+ assert result ["body" ] == {"name" : "John" , "age" : 30 }
357
+
358
+
359
+ def test_validate_response_invalid_return ():
360
+ # GIVEN an APIGatewayRestResolver with validation enabled
361
+ app = APIGatewayRestResolver (enable_validation = True )
362
+
363
+ class Model (BaseModel ):
364
+ name : str
365
+ age : int
366
+
367
+ # WHEN a handler is defined with a body parameter
368
+ @app .post ("/" )
369
+ def handler (user : Model ) -> Response [Model ]:
370
+ return Response (body = user , status_code = 200 )
371
+
372
+ LOAD_GW_EVENT ["httpMethod" ] = "POST"
373
+ LOAD_GW_EVENT ["path" ] = "/"
374
+ LOAD_GW_EVENT ["body" ] = json .dumps ({})
375
+
376
+ # THEN the handler should be invoked and return 422
377
+ # THEN the body should have the word missing
378
+ result = app (LOAD_GW_EVENT , {})
379
+ assert result ["statusCode" ] == 422
380
+ assert "missing" in result ["body" ]
0 commit comments