Skip to content

Commit

Permalink
fix: primitive serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
xgui3783 committed Dec 11, 2024
1 parent 7287f54 commit b330147
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion api/serialization/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def instance_to_model(instance: Any, * , use_class: Type=None, skip_classes: Lis

if instance is None:
return None
if isinstance(instance, (str, int, float)):
if isinstance(instance, str):
return instance
if isinstance(instance, (int, float)):
if math.isnan(instance):
return None
return instance
Expand Down
24 changes: 24 additions & 0 deletions test/serialization/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest
import math

from api.serialization.util import instance_to_model

@pytest.mark.parametrize("target, result, error", [
(None, None, None),
("foo", "foo", None),
(1, 1, None),
(0.1, 0.1, None),
(math.nan, None, None),
([1, 2], [1, 2], None),
((1, 2), [1, 2], None),
({"foo": "bar", "one": 2}, {"foo": "bar", "one": 2}, None),
({"foo": [1, "bar"]}, {"foo": [1, "bar"]}, None),
({"foo": (1, "bar")}, {"foo": [1, "bar"]}, None),
])
def test_instance_to_model(target, result, error):
if error:
with pytest.raises(error):
instance_to_model(target)
pass
return
assert result == instance_to_model(target)

0 comments on commit b330147

Please sign in to comment.