File tree 2 files changed +28
-0
lines changed
aws_lambda_powertools/event_handler/openapi
tests/functional/event_handler/_pydantic 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,7 @@ def jsonable_encoder( # noqa: PLR0911
81
81
exclude_unset = exclude_unset ,
82
82
exclude_none = exclude_none ,
83
83
exclude_defaults = exclude_defaults ,
84
+ custom_serializer = custom_serializer ,
84
85
)
85
86
86
87
# Dataclasses
@@ -171,6 +172,7 @@ def _dump_base_model(
171
172
exclude_unset : bool = False ,
172
173
exclude_none : bool = False ,
173
174
exclude_defaults : bool = False ,
175
+ custom_serializer : Optional [Callable [[Any ], str ]] = None ,
174
176
):
175
177
"""
176
178
Dump a BaseModel object to a dict, using the same parameters as jsonable_encoder
@@ -192,6 +194,7 @@ def _dump_base_model(
192
194
obj_dict ,
193
195
exclude_none = exclude_none ,
194
196
exclude_defaults = exclude_defaults ,
197
+ custom_serializer = custom_serializer ,
195
198
)
196
199
197
200
Original file line number Diff line number Diff line change @@ -228,3 +228,28 @@ def serializer(value):
228
228
229
229
# THEN we should get the custom serializer output
230
230
assert result == ["serialized" ]
231
+
232
+
233
+ def test_openapi_encode_custom_serializer_pydantic ():
234
+ # GIVEN a sequence with a custom class
235
+ class CustomClass :
236
+ __slots__ = []
237
+
238
+ class Order (BaseModel ):
239
+ kind : CustomClass
240
+
241
+ # maintenance: deprecate in V3; becomes model_config =ConfigDict(<directive>=True)
242
+ class Config :
243
+ arbitrary_types_allowed = True
244
+
245
+ order = Order (kind = CustomClass ())
246
+
247
+ # AND a custom serializer
248
+ def serializer (value ):
249
+ return "serialized"
250
+
251
+ # WHEN we call jsonable_encoder with the nested dictionary and unserializable value
252
+ result = jsonable_encoder (order , custom_serializer = serializer )
253
+
254
+ # THEN we should get the custom serializer output
255
+ assert result == {"kind" : "serialized" }
You can’t perform that action at this time.
0 commit comments