Skip to content

Commit 0287d75

Browse files
committed
Fix: Update serialization logic to handle Message instances and improve error handling in tests
1 parent 90f6365 commit 0287d75

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
### Fixed
2121
- Fix dataclass message serialization
22-
- Go back to best effort serialization type check is not forced
22+
- Go back to best effort serialization : type check is not forced
23+
- For checking use PydanticMessage
2324

2425
## [3.4.0] - 2025-03-24
2526

src/iop/_serialization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from . import _iris
1111
from pydantic import BaseModel, TypeAdapter, ValidationError
1212

13-
from iop._message import _PydanticPickleMessage
13+
from iop._message import _PydanticPickleMessage, _Message
1414
from iop._utils import _Utils
1515

1616
class SerializationError(Exception):
@@ -31,10 +31,10 @@ def _convert_to_json_safe(obj: Any) -> Any:
3131
"""Convert objects to JSON-safe format."""
3232
if isinstance(obj, BaseModel):
3333
return obj.model_dump_json()
34-
elif is_dataclass(obj):
34+
elif is_dataclass(obj) and isinstance(obj, _Message):
3535
return TempPydanticModel.model_validate(dataclass_to_dict(obj)).model_dump_json()
3636
else:
37-
raise SerializationError(f"Object {obj} must be a Pydantic model or dataclass")
37+
raise SerializationError(f"Object {obj} must be a Pydantic model or dataclass Message")
3838

3939
@staticmethod
4040
def serialize(message: Any, use_pickle: bool = False) -> Any:

src/tests/test_serialization.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@
1515
deserialize_pickle_message,
1616
)
1717

18+
from iop import Message
19+
1820
class NonDataclass:
1921
def __init__(self, value):
2022
self.value = value
2123

24+
@dataclass
25+
class Empty(Message):
26+
pass
27+
2228
@dataclass
2329
class Object:
2430
value: str
2531

2632
@dataclass
27-
class FullMessge:
33+
class FullMessge(Message):
34+
"""Full message class with various fields."""
2835
text: str
2936
dikt: dict
3037
text_json: str
@@ -48,11 +55,28 @@ class MyObject:
4855
bar: float = 3.14
4956

5057
@dataclass
51-
class Msg:
58+
class Msg(Message):
5259
text: str
5360
number: int
5461
my_obj: MyObject
5562

63+
def test_empty_serialization():
64+
# Create an empty message
65+
msg = Empty()
66+
msg.foo = 42
67+
msg.bar = "hello"
68+
69+
# Test serialization
70+
serial = serialize_message(msg)
71+
assert type(serial).__module__.startswith('iris') and serial._IsA("IOP.Message")
72+
assert serial.classname == f"{Empty.__module__}.{Empty.__name__}"
73+
74+
# Test deserialization
75+
result = deserialize_message(serial)
76+
assert isinstance(result, Empty)
77+
assert result.foo == msg.foo
78+
assert result.bar == msg.bar
79+
5680
def test_message_serialization():
5781
msg = Msg(text="hello", number=42, my_obj=None)
5882

@@ -76,6 +100,15 @@ def test_message_serialization():
76100
assert result.number == msg.number
77101
assert result.my_obj == my_obj
78102

103+
def test_raise_not_message():
104+
# Create a message with an invalid object
105+
msg = MyObject(value="test", foo=None)
106+
107+
# Test serialization
108+
with pytest.raises(SerializationError):
109+
serialize_message(msg)
110+
111+
79112
def test_unexpexted_obj_serialization():
80113
# Create an invalid message
81114
msg = Msg(text="hello", number=42, my_obj=None)

0 commit comments

Comments
 (0)