@@ -546,20 +546,20 @@ def body_field(self) -> ModelField | None:
546546
547547 return self ._body_field
548548
549- def _get_openapi_path (
549+ def _get_openapi_path ( # noqa PLR0912
550550 self ,
551551 * ,
552552 dependant : Dependant ,
553553 operation_ids : set [str ],
554554 model_name_map : dict [TypeModelOrEnum , str ],
555555 field_mapping : dict [tuple [ModelField , Literal ["validation" , "serialization" ]], JsonSchemaValue ],
556+ enable_validation : bool = False ,
556557 ) -> tuple [dict [str , Any ], dict [str , Any ]]:
557558 """
558559 Returns the OpenAPI path and definitions for the route.
559560 """
560561 from aws_lambda_powertools .event_handler .openapi .dependant import get_flat_params
561562
562- path = {}
563563 definitions : dict [str , Any ] = {}
564564
565565 # Gather all the route parameters
@@ -598,13 +598,18 @@ def _get_openapi_path(
598598 if request_body_oai :
599599 operation ["requestBody" ] = request_body_oai
600600
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+ }
608613
609614 # Add custom response validation response, if exists
610615 if self .custom_response_validation_http_code :
@@ -681,8 +686,7 @@ def _get_openapi_path(
681686 }
682687
683688 operation ["responses" ] = operation_responses
684- path [self .method .lower ()] = operation
685-
689+ path = {self .method .lower (): operation }
686690 # Add the validation error schema to the definitions, but only if it hasn't been added yet
687691 if "ValidationError" not in definitions :
688692 definitions .update (
@@ -1834,6 +1838,7 @@ def get_openapi_schema(
18341838 operation_ids = operation_ids ,
18351839 model_name_map = model_name_map ,
18361840 field_mapping = field_mapping ,
1841+ enable_validation = self ._enable_validation ,
18371842 )
18381843 if result :
18391844 path , path_definitions = self ._add_resolver_response_validation_error_response_to_route (result )
0 commit comments