Skip to content

Commit d678bd9

Browse files
committed
fix: empty dictionary when receiving null
1 parent 9d7ba54 commit d678bd9

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎src/dsf/object_model/utils.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def is_model_object(o: object) -> bool:
3333

3434

3535
def _set_model_prop(instance: object, name: str, runtime_type: type[JSONElement | datetime], current_value: T, value: Union[T, JSONElement]):
36-
if isinstance(value, dict): # Update from JSON
36+
if value is None and isinstance(current_value, ModelDictionary): # DSF sends null to clear a dictionary
37+
current_value.update_from_json(cast(JSONObj, None))
38+
elif isinstance(value, dict): # Update from JSON
3739
if not isinstance(current_value, (ModelObject, ModelDictionary)):
3840
raise TypeError(f"{instance.__class__.__name__}.{name} must be of type ModelObject or ModelDictionary to update from a dict."
3941
f" Got {type(current_value).__name__}: {current_value}")

‎tests/test_object_model.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,16 @@ def test_rc2_fields(self):
424424
model.update_from_json('{"move": {"shaping": {"type": "eI3"}}}')
425425
self.assertEqual(model.move.shaping.type, InputShapingType.ei3)
426426

427+
def test_null_clears_dictionary(self):
428+
# DSF sends null for a dictionary that has been cleared, e.g. job.file.customInfo when a job ends
429+
model = ObjectModel()
430+
model.update_from_json({"job": {"file": {"customInfo": {"material": "PETG"}}}})
431+
self.assertEqual(dict(model.job.file.custom_info), {"material": "PETG"})
432+
custom_info = model.job.file.custom_info
433+
model.update_from_json({"job": {"file": {"customInfo": None}}})
434+
self.assertIs(model.job.file.custom_info, custom_info)
435+
self.assertEqual(dict(model.job.file.custom_info), {})
436+
427437
def test_global(self):
428438
# "global" is a Python keyword so the JSON key is exposed as ObjectModel.globals
429439
with open('tests/object_model/model_full.json') as fp:

0 commit comments

Comments
 (0)