Skip to content

Commit ed584f4

Browse files
committed
fix: comments
1 parent 8d5081f commit ed584f4

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1402,8 +1402,8 @@ def __init__(
14021402
from aws_lambda_powertools.event_handler.middlewares.openapi_validation import OpenAPIValidationMiddleware
14031403

14041404
# Note the serializer argument: only use custom serializer if provided by the caller
1405-
# Otherwise, fully rely on the internal Pydantic based mechanism to serialize responses.
1406-
self.use([OpenAPIValidationMiddleware(response_serializer=serializer)])
1405+
# Otherwise, fully rely on the internal Pydantic based mechanism to serialize responses for validation.
1406+
self.use([OpenAPIValidationMiddleware(validation_serializer=serializer)])
14071407

14081408
def get_openapi_schema(
14091409
self,

aws_lambda_powertools/event_handler/middlewares/openapi_validation.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,17 @@ def get_todos(): List[Todo]:
5555
```
5656
"""
5757

58-
def __init__(self, response_serializer: Optional[Callable[[Any], str]] = None):
59-
self._response_serializer = response_serializer
58+
def __init__(self, validation_serializer: Optional[Callable[[Any], str]] = None):
59+
"""
60+
Initialize the OpenAPIValidationMiddleware.
61+
62+
Parameters
63+
----------
64+
validation_serializer : Callable, optional
65+
Optional serializer to use when serializing the response for validation.
66+
Use it when you have a custom type that cannot be serialized by the default jsonable_encoder.
67+
"""
68+
self._validation_serializer = validation_serializer
6069

6170
def handler(self, app: EventHandlerInstance, next_middleware: NextMiddleware) -> Response:
6271
logger.debug("OpenAPIValidationMiddleware handler")
@@ -184,11 +193,11 @@ def _serialize_response(
184193
exclude_unset=exclude_unset,
185194
exclude_defaults=exclude_defaults,
186195
exclude_none=exclude_none,
187-
custom_serializer=self._response_serializer,
196+
custom_serializer=self._validation_serializer,
188197
)
189198
else:
190199
# Just serialize the response content returned from the handler
191-
return jsonable_encoder(response_content, custom_serializer=self._response_serializer)
200+
return jsonable_encoder(response_content, custom_serializer=self._validation_serializer)
192201

193202
def _prepare_response_content(
194203
self,

aws_lambda_powertools/event_handler/openapi/encoders.py

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def jsonable_encoder( # noqa: PLR0911
5656
by default False
5757
exclude_none : bool, optional
5858
Whether fields that are equal to None should be excluded, by default False
59+
custom_serializer : Callable, optional
60+
A custom serializer to use for encoding the object, when everything else fails.
5961
6062
Returns
6163
-------

0 commit comments

Comments
 (0)