Skip to content

Commit 873745d

Browse files
authored
Merge branch 'develop' into feat/per-route-validation
2 parents 1ca39ed + ea5c094 commit 873745d

File tree

10 files changed

+174
-95
lines changed

10 files changed

+174
-95
lines changed

.github/workflows/dependency-review.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
- name: 'Checkout Repository'
2323
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2424
- name: 'Dependency Review'
25-
uses: actions/dependency-review-action@3c4e3dcb1aa7874d2c16be7d79418e9b7efd6261 # v4.8.2
25+
uses: actions/dependency-review-action@05fe4576374b728f0c523d6a13d64c25081e0803 # v4.8.3

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,
@@ -2997,6 +2998,18 @@ def _call_exception_handler(self, exp: Exception, route: Route) -> ResponseBuild
29972998
route=route,
29982999
)
29993000

3001+
if isinstance(exp, RequestUnsupportedContentType):
3002+
errors = [{"loc": e["loc"], "type": e["type"]} for e in exp.errors()]
3003+
return self._response_builder_class(
3004+
response=Response(
3005+
status_code=HTTPStatus.UNSUPPORTED_MEDIA_TYPE,
3006+
content_type=content_types.APPLICATION_JSON,
3007+
body={"statusCode": HTTPStatus.UNSUPPORTED_MEDIA_TYPE, "detail": errors},
3008+
),
3009+
serializer=self._serializer,
3010+
route=route,
3011+
)
3012+
30003013
if isinstance(exp, ServiceError):
30013014
return self._response_builder_class(
30023015
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)

aws_lambda_powertools/utilities/parameters/ssm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,12 @@ def _get_parameters_by_name_in_chunks(
583583
diff = {key: value for key, value in batch.items() if key not in cache}
584584

585585
for chunk in slice_dictionary(data=diff, chunk_size=self._MAX_GET_PARAMETERS_ITEM):
586-
response, possible_errors = self._get_parameters_by_name(
586+
chunk_response, possible_errors = self._get_parameters_by_name(
587587
parameters=chunk,
588588
raise_on_error=raise_on_error,
589589
decrypt=decrypt,
590590
)
591-
response.update(response)
591+
response.update(chunk_response)
592592
errors.extend(possible_errors)
593593

594594
return response, errors

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "aws-lambda-powertools-python-e2e",
33
"version": "1.0.0",
44
"devDependencies": {
5-
"aws-cdk": "^2.1105.0"
5+
"aws-cdk": "^2.1106.0"
66
}
77
}

0 commit comments

Comments
 (0)