Skip to content

fix(event_handler): fix OpenAPI schema response for disabled validation #6720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading