diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index f1f38b399a9..38ea4c363dd 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -546,20 +546,20 @@ def body_field(self) -> ModelField | None: return self._body_field - def _get_openapi_path( + def _get_openapi_path( # noqa PLR0912 self, *, dependant: Dependant, operation_ids: set[str], model_name_map: dict[TypeModelOrEnum, str], field_mapping: dict[tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue], + enable_validation: bool = False, ) -> tuple[dict[str, Any], dict[str, Any]]: """ Returns the OpenAPI path and definitions for the route. """ from aws_lambda_powertools.event_handler.openapi.dependant import get_flat_params - path = {} definitions: dict[str, Any] = {} # Gather all the route parameters @@ -598,13 +598,18 @@ def _get_openapi_path( if request_body_oai: operation["requestBody"] = request_body_oai - # Validation failure response (422) will always be part of the schema - operation_responses: dict[int, OpenAPIResponse] = { - 422: { - "description": "Validation Error", - "content": {_DEFAULT_CONTENT_TYPE: {"schema": {"$ref": f"{COMPONENT_REF_PREFIX}HTTPValidationError"}}}, - }, - } + operation_responses: dict[int, OpenAPIResponse] = {} + + if enable_validation: + # Validation failure response (422) is added only if Enable Validation feature is true + operation_responses = { + 422: { + "description": "Validation Error", + "content": { + _DEFAULT_CONTENT_TYPE: {"schema": {"$ref": f"{COMPONENT_REF_PREFIX}HTTPValidationError"}}, + }, + }, + } # Add custom response validation response, if exists if self.custom_response_validation_http_code: @@ -681,8 +686,7 @@ def _get_openapi_path( } operation["responses"] = operation_responses - path[self.method.lower()] = operation - + path = {self.method.lower(): operation} # Add the validation error schema to the definitions, but only if it hasn't been added yet if "ValidationError" not in definitions: definitions.update( @@ -1834,6 +1838,7 @@ def get_openapi_schema( operation_ids=operation_ids, model_name_map=model_name_map, field_mapping=field_mapping, + enable_validation=self._enable_validation, ) if result: path, path_definitions = self._add_resolver_response_validation_error_response_to_route(result) diff --git a/tests/functional/event_handler/_pydantic/test_openapi_responses.py b/tests/functional/event_handler/_pydantic/test_openapi_responses.py index 0ac24dcc96b..8c41651f803 100644 --- a/tests/functional/event_handler/_pydantic/test_openapi_responses.py +++ b/tests/functional/event_handler/_pydantic/test_openapi_responses.py @@ -218,3 +218,22 @@ def another_handler(): assert 417 in responses_with_resolver_response_validation assert 418 not in responses_with_resolver_response_validation assert responses_with_resolver_response_validation[417].description == "Response Validation Error" + + +def test_openapi_enable_validation_disabled(): + # GIVEN An API Gateway resolver without validation + app = APIGatewayRestResolver() + + @app.get("/") + def handler(): + pass + + # WHEN we retrieve the OpenAPI schema for the application + schema = app.get_openapi_schema() + responses = schema.paths["/"].get.responses + + # THE the schema should include a 200 successful response + # but not a 422 validation error response since validation is disabled + assert 200 in responses.keys() + assert responses[200].description == "Successful Response" + assert 422 not in responses.keys()