Skip to content

Commit af47df2

Browse files
Merge branch 'develop' into dependabot/pip/develop/types-python-dateutil-2.9.0.20260124
2 parents e8cff5c + c4434b7 commit af47df2

File tree

5 files changed

+119
-61
lines changed

5 files changed

+119
-61
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
DEFAULT_OPENAPI_VERSION,
2828
)
2929
from aws_lambda_powertools.event_handler.openapi.exceptions import (
30+
RequestUnsupportedContentType,
3031
RequestValidationError,
3132
ResponseValidationError,
3233
SchemaValidationError,
@@ -2972,6 +2973,18 @@ def _call_exception_handler(self, exp: Exception, route: Route) -> ResponseBuild
29722973
route=route,
29732974
)
29742975

2976+
if isinstance(exp, RequestUnsupportedContentType):
2977+
errors = [{"loc": e["loc"], "type": e["type"]} for e in exp.errors()]
2978+
return self._response_builder_class(
2979+
response=Response(
2980+
status_code=HTTPStatus.UNSUPPORTED_MEDIA_TYPE,
2981+
content_type=content_types.APPLICATION_JSON,
2982+
body={"statusCode": HTTPStatus.UNSUPPORTED_MEDIA_TYPE, "detail": errors},
2983+
),
2984+
serializer=self._serializer,
2985+
route=route,
2986+
)
2987+
29752988
if isinstance(exp, ServiceError):
29762989
return self._response_builder_class(
29772990
response=Response(

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
)
2020
from aws_lambda_powertools.event_handler.openapi.dependant import is_scalar_field
2121
from aws_lambda_powertools.event_handler.openapi.encoders import jsonable_encoder
22-
from aws_lambda_powertools.event_handler.openapi.exceptions import RequestValidationError, ResponseValidationError
22+
from aws_lambda_powertools.event_handler.openapi.exceptions import (
23+
RequestUnsupportedContentType,
24+
RequestValidationError,
25+
ResponseValidationError,
26+
)
2327
from aws_lambda_powertools.event_handler.openapi.params import Param
2428

2529
if TYPE_CHECKING:
@@ -129,7 +133,18 @@ def _get_body(self, app: EventHandlerInstance) -> dict[str, Any]:
129133
return self._parse_form_data(app)
130134

131135
else:
132-
raise NotImplementedError("Only JSON body or Form() are supported")
136+
raise RequestUnsupportedContentType(
137+
"Only JSON body or Form() are supported",
138+
errors=[
139+
{
140+
"type": "unsupported_content_type",
141+
"loc": ("body",),
142+
"msg": "Only JSON body or Form() are supported",
143+
"input": {},
144+
"ctx": {},
145+
},
146+
],
147+
)
133148

134149
def _parse_json_data(self, app: EventHandlerInstance) -> dict[str, Any]:
135150
"""Parse JSON data from the request body."""

aws_lambda_powertools/event_handler/openapi/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ class SchemaValidationError(ValidationException):
4949

5050
class OpenAPIMergeError(Exception):
5151
"""Exception raised when there's a conflict during OpenAPI merge."""
52+
53+
54+
class RequestUnsupportedContentType(NotImplementedError, ValidationException):
55+
"""Exception raised when trying to read request body data, with unknown headers"""
56+
57+
# REVIEW: This inheritance is for backwards compatibility.
58+
# Just inherit from ValidationException in Powertools V4
59+
def __init__(self, msg: str, errors: Sequence[Any]) -> None:
60+
NotImplementedError.__init__(self, msg)
61+
ValidationException.__init__(self, errors)

0 commit comments

Comments
 (0)