@@ -1500,7 +1500,7 @@ def __init__(
1500
1500
serializer : Callable [[dict ], str ] | None = None ,
1501
1501
strip_prefixes : list [str | Pattern ] | None = None ,
1502
1502
enable_validation : bool = False ,
1503
- response_validation_error_http_status : HTTPStatus | None = None ,
1503
+ response_validation_error_http_status = None ,
1504
1504
):
1505
1505
"""
1506
1506
Parameters
@@ -1520,6 +1520,8 @@ def __init__(
1520
1520
Each prefix can be a static string or a compiled regex pattern
1521
1521
enable_validation: bool | None
1522
1522
Enables validation of the request body against the route schema, by default False.
1523
+ response_validation_error_http_status
1524
+ Enables response validation and sets returned status code if response is not validated.
1523
1525
"""
1524
1526
self ._proxy_type = proxy_type
1525
1527
self ._dynamic_routes : list [Route ] = []
@@ -1535,7 +1537,28 @@ def __init__(
1535
1537
self .context : dict = {} # early init as customers might add context before event resolution
1536
1538
self .processed_stack_frames = []
1537
1539
self ._response_builder_class = ResponseBuilder [BaseProxyEvent ]
1538
- self ._response_validation_error_http_status = response_validation_error_http_status
1540
+ self ._has_response_validation_error = response_validation_error_http_status is not None
1541
+
1542
+ if response_validation_error_http_status and not enable_validation :
1543
+ msg = "'response_validation_error_http_status' cannot be set when enable_validation is False."
1544
+ raise ValueError (msg )
1545
+
1546
+ if (
1547
+ not isinstance (response_validation_error_http_status , HTTPStatus )
1548
+ and response_validation_error_http_status is not None
1549
+ ):
1550
+
1551
+ try :
1552
+ response_validation_error_http_status = HTTPStatus (response_validation_error_http_status )
1553
+ except ValueError :
1554
+ msg = f"'{ response_validation_error_http_status } ' must be an integer representing an HTTP status code."
1555
+ raise ValueError (msg ) from None
1556
+
1557
+ self ._response_validation_error_http_status = (
1558
+ response_validation_error_http_status
1559
+ if response_validation_error_http_status
1560
+ else HTTPStatus .UNPROCESSABLE_ENTITY
1561
+ )
1539
1562
1540
1563
# Allow for a custom serializer or a concise json serialization
1541
1564
self ._serializer = serializer or partial (json .dumps , separators = ("," , ":" ), cls = Encoder )
@@ -1549,7 +1572,7 @@ def __init__(
1549
1572
[
1550
1573
OpenAPIValidationMiddleware (
1551
1574
validation_serializer = serializer ,
1552
- has_response_validation_error = self ._response_validation_error_http_status is not None ,
1575
+ has_response_validation_error = self ._has_response_validation_error ,
1553
1576
),
1554
1577
],
1555
1578
)
@@ -2386,12 +2409,15 @@ def _call_exception_handler(self, exp: Exception, route: Route) -> ResponseBuild
2386
2409
# OpenAPIValidationMiddleware will only raise ResponseValidationError when
2387
2410
# 'self._response_validation_error_http_status' is not None
2388
2411
if isinstance (exp , ResponseValidationError ):
2389
- if self ._response_validation_error_http_status is None :
2390
- raise TypeError
2412
+ http_status = (
2413
+ self ._response_validation_error_http_status
2414
+ if self ._response_validation_error_http_status
2415
+ else HTTPStatus .UNPROCESSABLE_ENTITY
2416
+ )
2391
2417
errors = [{"loc" : e ["loc" ], "type" : e ["type" ]} for e in exp .errors ()]
2392
2418
return self ._response_builder_class (
2393
2419
response = Response (
2394
- status_code = self . _response_validation_error_http_status ,
2420
+ status_code = http_status . value ,
2395
2421
content_type = content_types .APPLICATION_JSON ,
2396
2422
body = {"statusCode" : self ._response_validation_error_http_status , "detail" : errors },
2397
2423
),
@@ -2611,7 +2637,7 @@ def __init__(
2611
2637
serializer : Callable [[dict ], str ] | None = None ,
2612
2638
strip_prefixes : list [str | Pattern ] | None = None ,
2613
2639
enable_validation : bool = False ,
2614
- response_validation_error_http_status : HTTPStatus | None = None ,
2640
+ response_validation_error_http_status : HTTPStatus | int | None = None ,
2615
2641
):
2616
2642
"""Amazon API Gateway REST and HTTP API v1 payload resolver"""
2617
2643
super ().__init__ (
@@ -2695,7 +2721,7 @@ def __init__(
2695
2721
serializer : Callable [[dict ], str ] | None = None ,
2696
2722
strip_prefixes : list [str | Pattern ] | None = None ,
2697
2723
enable_validation : bool = False ,
2698
- response_validation_error_http_status : HTTPStatus | None = None ,
2724
+ response_validation_error_http_status : HTTPStatus | int | None = None ,
2699
2725
):
2700
2726
"""Amazon API Gateway HTTP API v2 payload resolver"""
2701
2727
super ().__init__ (
@@ -2734,7 +2760,7 @@ def __init__(
2734
2760
serializer : Callable [[dict ], str ] | None = None ,
2735
2761
strip_prefixes : list [str | Pattern ] | None = None ,
2736
2762
enable_validation : bool = False ,
2737
- response_validation_error_http_status : HTTPStatus | None = None ,
2763
+ response_validation_error_http_status : HTTPStatus | int | None = None ,
2738
2764
):
2739
2765
"""Amazon Application Load Balancer (ALB) resolver"""
2740
2766
super ().__init__ (
0 commit comments