Skip to content

Commit fea8757

Browse files
odesenfanshoh
authored andcommitted
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.
1 parent 01e891a commit fea8757

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/aleph/sdk/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from aleph_message.models.execution.base import Encoding
4848
from aleph_message.status import MessageStatus
4949
from pydantic import ValidationError
50+
from pydantic.json import pydantic_encoder
5051

5152
from aleph.sdk.types import Account, GenericMessage, StorageEnum
5253
from aleph.sdk.utils import Writable, copy_async_readable_to_buffer
@@ -1475,7 +1476,10 @@ async def _prepare_aleph_message(
14751476
"channel": channel,
14761477
}
14771478

1478-
item_content: str = json.dumps(content, separators=(",", ":"))
1479+
# Use the Pydantic encoder to serialize types like UUID, datetimes, etc.
1480+
item_content: str = json.dumps(
1481+
content, separators=(",", ":"), default=pydantic_encoder
1482+
)
14791483

14801484
if allow_inlining and (len(item_content) < settings.MAX_INLINE_SIZE):
14811485
message_dict["item_content"] = item_content

0 commit comments

Comments
 (0)