Skip to content

Commit 5345fd2

Browse files
committed
fix(idempotency): fix serialization of Pydantic idempotency keys
1 parent 0309d53 commit 5345fd2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

aws_lambda_powertools/utilities/idempotency/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _prepare_data(data: Any) -> Any:
6060

6161
# Convert from Pydantic model
6262
if callable(getattr(data, "model_dump", None)):
63-
return data.model_dump()
63+
return data.model_dump(mode="json")
6464

6565
# Convert from event source data class
6666
if callable(getattr(data, "dict", None)):

tests/functional/idempotency/_pydantic/test_idempotency_with_pydantic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Optional
2+
from uuid import UUID
23

34
import pytest
45
from pydantic import BaseModel
@@ -194,6 +195,19 @@ class Foo(BaseModel):
194195
assert as_dict == expected_result
195196

196197

198+
def test_idempotent_function_pydantic_uuid():
199+
# Scenario _prepare_data should convert a pydantic model with UUIDs to a dict
200+
# with serialization of UUIDs to strings
201+
class Foo(BaseModel):
202+
uuid: UUID
203+
204+
uuid = UUID("01234567-0123-0123-0123-0123456789ab")
205+
expected_result = {"uuid": str(uuid)}
206+
data = Foo(uuid=uuid)
207+
as_dict = _prepare_data(data)
208+
assert as_dict == expected_result
209+
210+
197211
@pytest.mark.parametrize("data", [None, "foo", ["foo"], 1, True, {}])
198212
def test_idempotent_function_other(data):
199213
# All other data types should be left as is

0 commit comments

Comments
 (0)