From fea87572312c894acb60c59fc7c394401a79edad Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Fri, 22 Sep 2023 17:36:04 +0200 Subject: [PATCH] Fix: allow user to specify datetime and UUID objects in post content Problem: using `create_post()` with an object containing datetime, UUIDs and other types not serializable by the Python `json` module fails. Solution: as we already use Pydantic all over the place, use the Pydantic encoder as default for all the types not serializable by the `json` module. --- src/aleph/sdk/client.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/aleph/sdk/client.py b/src/aleph/sdk/client.py index 520e8bd7..c51ca6dc 100644 --- a/src/aleph/sdk/client.py +++ b/src/aleph/sdk/client.py @@ -47,6 +47,7 @@ from aleph_message.models.execution.base import Encoding from aleph_message.status import MessageStatus from pydantic import ValidationError +from pydantic.json import pydantic_encoder from aleph.sdk.types import Account, GenericMessage, StorageEnum from aleph.sdk.utils import Writable, copy_async_readable_to_buffer @@ -1475,7 +1476,10 @@ async def _prepare_aleph_message( "channel": channel, } - item_content: str = json.dumps(content, separators=(",", ":")) + # Use the Pydantic encoder to serialize types like UUID, datetimes, etc. + item_content: str = json.dumps( + content, separators=(",", ":"), default=pydantic_encoder + ) if allow_inlining and (len(item_content) < settings.MAX_INLINE_SIZE): message_dict["item_content"] = item_content