@@ -546,20 +546,20 @@ def body_field(self) -> ModelField | None:
546
546
547
547
return self ._body_field
548
548
549
- def _get_openapi_path (
549
+ def _get_openapi_path ( # noqa PLR0912
550
550
self ,
551
551
* ,
552
552
dependant : Dependant ,
553
553
operation_ids : set [str ],
554
554
model_name_map : dict [TypeModelOrEnum , str ],
555
555
field_mapping : dict [tuple [ModelField , Literal ["validation" , "serialization" ]], JsonSchemaValue ],
556
+ enable_validation : bool = False ,
556
557
) -> tuple [dict [str , Any ], dict [str , Any ]]:
557
558
"""
558
559
Returns the OpenAPI path and definitions for the route.
559
560
"""
560
561
from aws_lambda_powertools .event_handler .openapi .dependant import get_flat_params
561
562
562
- path = {}
563
563
definitions : dict [str , Any ] = {}
564
564
565
565
# Gather all the route parameters
@@ -598,13 +598,18 @@ def _get_openapi_path(
598
598
if request_body_oai :
599
599
operation ["requestBody" ] = request_body_oai
600
600
601
- # Validation failure response (422) will always be part of the schema
602
- operation_responses : dict [int , OpenAPIResponse ] = {
603
- 422 : {
604
- "description" : "Validation Error" ,
605
- "content" : {_DEFAULT_CONTENT_TYPE : {"schema" : {"$ref" : f"{ COMPONENT_REF_PREFIX } HTTPValidationError" }}},
606
- },
607
- }
601
+ operation_responses : dict [int , OpenAPIResponse ] = {}
602
+
603
+ if enable_validation :
604
+ # Validation failure response (422) is added only if Enable Validation feature is true
605
+ operation_responses = {
606
+ 422 : {
607
+ "description" : "Validation Error" ,
608
+ "content" : {
609
+ _DEFAULT_CONTENT_TYPE : {"schema" : {"$ref" : f"{ COMPONENT_REF_PREFIX } HTTPValidationError" }},
610
+ },
611
+ },
612
+ }
608
613
609
614
# Add custom response validation response, if exists
610
615
if self .custom_response_validation_http_code :
@@ -681,8 +686,7 @@ def _get_openapi_path(
681
686
}
682
687
683
688
operation ["responses" ] = operation_responses
684
- path [self .method .lower ()] = operation
685
-
689
+ path = {self .method .lower (): operation }
686
690
# Add the validation error schema to the definitions, but only if it hasn't been added yet
687
691
if "ValidationError" not in definitions :
688
692
definitions .update (
@@ -1834,6 +1838,7 @@ def get_openapi_schema(
1834
1838
operation_ids = operation_ids ,
1835
1839
model_name_map = model_name_map ,
1836
1840
field_mapping = field_mapping ,
1841
+ enable_validation = self ._enable_validation ,
1837
1842
)
1838
1843
if result :
1839
1844
path , path_definitions = self ._add_resolver_response_validation_error_response_to_route (result )
0 commit comments