From fd014d20261a9799cf655797f65d19a2557dfa2a Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Sat, 2 Nov 2024 16:37:31 +0200 Subject: [PATCH] Fixed UUID (and other types) serialization in the CallbackData factory. (#1602) From now UUID will have 32 bytes length instead of 36 bytes. --- CHANGES/1602.bugfix.rst | 3 +++ aiogram/filters/callback_data.py | 2 +- tests/test_filters/test_callback_data.py | 12 ++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 CHANGES/1602.bugfix.rst diff --git a/CHANGES/1602.bugfix.rst b/CHANGES/1602.bugfix.rst new file mode 100644 index 00000000..917d927c --- /dev/null +++ b/CHANGES/1602.bugfix.rst @@ -0,0 +1,3 @@ +Fixed customized serialization in the :class:`aiogram.filters.callback_data.CallbackData` factory. + +From now UUID will have 32 bytes length instead of 36 bytes (with no `-` separators) in the callback data representation. diff --git a/aiogram/filters/callback_data.py b/aiogram/filters/callback_data.py index 17ccffbf..c60cd818 100644 --- a/aiogram/filters/callback_data.py +++ b/aiogram/filters/callback_data.py @@ -94,7 +94,7 @@ class CallbackData(BaseModel): :return: valid callback data for Telegram Bot API """ result = [self.__prefix__] - for key, value in self.model_dump(mode="json").items(): + for key, value in self.model_dump(mode="python").items(): encoded = self._encode_value(key, value) if self.__separator__ in encoded: raise ValueError( diff --git a/tests/test_filters/test_callback_data.py b/tests/test_filters/test_callback_data.py index 635b8e9f..8cc2f51f 100644 --- a/tests/test_filters/test_callback_data.py +++ b/tests/test_filters/test_callback_data.py @@ -92,6 +92,18 @@ class TestCallbackData: assert MyCallback(foo="test", bar=42).pack() == "test:test:42" + def test_pack_uuid(self): + class MyCallbackWithUUID(CallbackData, prefix="test"): + foo: str + bar: UUID + + callback = MyCallbackWithUUID( + foo="test", + bar=UUID("123e4567-e89b-12d3-a456-426655440000"), + ) + + assert callback.pack() == "test:test:123e4567e89b12d3a456426655440000" + def test_pack_optional(self): class MyCallback1(CallbackData, prefix="test1"): foo: str