diff --git a/mira/metamodel/templates.py b/mira/metamodel/templates.py index 7e8ce6c5..f5d4a209 100644 --- a/mira/metamodel/templates.py +++ b/mira/metamodel/templates.py @@ -458,7 +458,7 @@ def from_json(cls, data, rate_symbols=None) -> "Template": @field_serializer('rate_law') def serialize_expression(self, rate_law): - return str(rate_law) + return str(rate_law) if rate_law is not None else None def is_equal_to(self, other: "Template", with_context: bool = False, config: Config = None) -> bool: diff --git a/tests/test_model_api.py b/tests/test_model_api.py index e5f51bec..b8a39611 100644 --- a/tests/test_model_api.py +++ b/tests/test_model_api.py @@ -589,18 +589,19 @@ def test_n_way_comparison_askenet(self): "exclude_defaults": True, "exclude_unset": True, "exclude_none": True, - # This key is outdated in Pydantic2 - # "skip_defaults": True, } # Compare the ModelComparisonResponse models # If assertion fails the diff is printed + # Use model dump as Pydantic objects have an attribute + # "model_fields_set" that contain explicitly set attributes even if + # are None assert local_response.model_dump() == resp_model.model_dump() local_sorted_str = sorted_json_str( - json.loads(local_response.model_dump_json()), + json.loads(local_response.model_dump_json(**dict_options)), skip_empty=True ) resp_sorted_str = sorted_json_str( - json.loads(resp_model.model_dump_json()), + json.loads(resp_model.model_dump_json(**dict_options)), skip_empty=True ) self.assertEqual(local_sorted_str, resp_sorted_str)