Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit ecf40f7

Browse files
Jordi Collelllovelydinosaur
authored andcommitted
fix bug on Decimal serialize with null values (#74)
* fix bug on Decimal deserialitzation with null values * fix 0 case
1 parent 5249c8a commit ecf40f7

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

tests/test_schemas.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,18 @@ class Guest(typesystem.Schema):
202202
def test_schema_decimal_serialization():
203203
class InventoryItem(typesystem.Schema):
204204
name = typesystem.String()
205-
price = typesystem.Decimal(precision="0.01")
205+
price = typesystem.Decimal(precision="0.01", allow_null=True)
206206

207207
item = InventoryItem(name="Example", price=123.45)
208208

209209
assert item.price == decimal.Decimal("123.45")
210210
assert item["price"] == 123.45
211211

212+
item = InventoryItem(name="test")
213+
assert dict(item) == {"name": "test", "price": None}
214+
item = InventoryItem(name="test", price=0)
215+
assert dict(item) == {"name": "test", "price": 0}
216+
212217

213218
def test_schema_uuid_serialization():
214219
class User(typesystem.Schema):

typesystem/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class Decimal(Number):
310310
numeric_type = decimal.Decimal
311311

312312
def serialize(self, obj: typing.Any) -> typing.Any:
313-
return float(obj)
313+
return None if obj is None else float(obj)
314314

315315

316316
class Boolean(Field):

0 commit comments

Comments
 (0)