Skip to content

Commit

Permalink
Fix: allow user to specify datetime and UUID objects in post content
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
odesenfans committed Sep 22, 2023
1 parent 01e891a commit 28a1051
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/aleph/sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 28a1051

Please sign in to comment.