Skip to content

Commit 818ad49

Browse files
fix(event_handler): fix OpenAPI schema response for disabled validation (#6720)
Removing 422 schema when validation is off
1 parent f64637a commit 818ad49

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tests/functional/event_handler/_pydantic/test_openapi_responses.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,22 @@ def another_handler():
218218
assert 417 in responses_with_resolver_response_validation
219219
assert 418 not in responses_with_resolver_response_validation
220220
assert responses_with_resolver_response_validation[417].description == "Response Validation Error"
221+
222+
223+
def test_openapi_enable_validation_disabled():
224+
# GIVEN An API Gateway resolver without validation
225+
app = APIGatewayRestResolver()
226+
227+
@app.get("/")
228+
def handler():
229+
pass
230+
231+
# WHEN we retrieve the OpenAPI schema for the application
232+
schema = app.get_openapi_schema()
233+
responses = schema.paths["/"].get.responses
234+
235+
# THE the schema should include a 200 successful response
236+
# but not a 422 validation error response since validation is disabled
237+
assert 200 in responses.keys()
238+
assert responses[200].description == "Successful Response"
239+
assert 422 not in responses.keys()

0 commit comments

Comments
 (0)