Skip to content

Commit b353426

Browse files
committed
fix(event_handler): always add 422 response to the schema
1 parent 0082a67 commit b353426

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,23 @@ def _get_openapi_path(
503503
if request_body_oai:
504504
operation["requestBody"] = request_body_oai
505505

506+
# Validation failure response (422) will always be part of the schema
507+
default_responses: Dict[int, OpenAPIResponse] = {
508+
422: {
509+
"description": "Validation Error",
510+
"content": {
511+
"application/json": {
512+
"schema": {"$ref": COMPONENT_REF_PREFIX + "HTTPValidationError"},
513+
},
514+
},
515+
},
516+
}
517+
506518
# Add the response to the OpenAPI operation
507519
if self.responses:
520+
# Merge default responses with user responses
521+
self.responses = {**default_responses, **self.responses}
522+
508523
for status_code in list(self.responses):
509524
response = self.responses[status_code]
510525

@@ -552,8 +567,7 @@ def _get_openapi_path(
552567
operation["responses"] = self.responses
553568
else:
554569
# Set the default 200 response
555-
responses = operation.setdefault("responses", {})
556-
success_response = responses.setdefault(200, {})
570+
success_response = default_responses.setdefault(200, {})
557571
success_response["description"] = self.response_description or _DEFAULT_OPENAPI_RESPONSE_DESCRIPTION
558572
success_response["content"] = {"application/json": {"schema": {}}}
559573
json_response = success_response["content"].setdefault("application/json", {})
@@ -567,24 +581,16 @@ def _get_openapi_path(
567581
),
568582
)
569583

570-
# Add validation failure response (422)
571-
operation["responses"][422] = {
572-
"description": "Validation Error",
573-
"content": {
574-
"application/json": {
575-
"schema": {"$ref": COMPONENT_REF_PREFIX + "HTTPValidationError"},
576-
},
577-
},
578-
}
584+
operation["responses"] = default_responses
579585

580-
# Add the validation error schema to the definitions, but only if it hasn't been added yet
581-
if "ValidationError" not in definitions:
582-
definitions.update(
583-
{
584-
"ValidationError": validation_error_definition,
585-
"HTTPValidationError": validation_error_response_definition,
586-
},
587-
)
586+
# Add the validation error schema to the definitions, but only if it hasn't been added yet
587+
if "ValidationError" not in definitions:
588+
definitions.update(
589+
{
590+
"ValidationError": validation_error_definition,
591+
"HTTPValidationError": validation_error_response_definition,
592+
},
593+
)
588594

589595
path[self.method.lower()] = operation
590596

aws_lambda_powertools/event_handler/openapi/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"type": "array",
2929
"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
3030
},
31-
"msg": {"title": "Message", "type": "string"},
31+
# For security reasons, we hide **msg** details (don't leak Python, Pydantic or filenames)
3232
"type": {"title": "Error Type", "type": "string"},
3333
},
3434
"required": ["loc", "msg", "type"],

tests/functional/event_handler/test_openapi_responses.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def handler():
5050
assert 202 in responses.keys()
5151
assert responses[202].description == "Custom response"
5252

53-
assert 200 not in responses.keys()
54-
assert 422 not in responses.keys()
53+
assert 200 not in responses.keys() # 200 was not added due to custom responses
54+
assert 422 in responses.keys() # 422 is always added due to potential data validation errors
5555

5656

5757
def test_openapi_200_custom_schema():

0 commit comments

Comments
 (0)