From fca36585a39ce15a48204f7362a9c49f164723d2 Mon Sep 17 00:00:00 2001
From: David Douard <david.douard@sdfa3.org>
Date: Mon, 8 Mar 2021 16:11:22 +0100
Subject: [PATCH] Fix MetadataAuthority.from_dict()

was modifying the dict given as argument.
---
 swh/model/model.py            |  5 ++++-
 swh/model/tests/test_model.py | 12 ++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/swh/model/model.py b/swh/model/model.py
index 6df310a0..1c46f2a0 100644
--- a/swh/model/model.py
+++ b/swh/model/model.py
@@ -848,7 +848,10 @@ class MetadataAuthority(BaseModel):
 
     @classmethod
     def from_dict(cls, d):
-        d["type"] = MetadataAuthorityType(d["type"])
+        d = {
+            **d,
+            "type": MetadataAuthorityType(d["type"]),
+        }
         return super().from_dict(d)
 
     def unique_key(self) -> KeyType:
diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py
index 2c84d714..bf9725c6 100644
--- a/swh/model/tests/test_model.py
+++ b/swh/model/tests/test_model.py
@@ -45,6 +45,7 @@ from swh.model.model import (
     Timestamp,
     TimestampWithTimezone,
 )
+from swh.model.tests.swh_model_data import TEST_OBJECTS
 from swh.model.tests.test_identifiers import (
     content_example,
     directory_example,
@@ -78,6 +79,17 @@ def test_todict_inverse_fromdict(objtype_and_obj):
     assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict()
 
 
+@pytest.mark.parametrize("object_type, objects", TEST_OBJECTS.items())
+def test_swh_model_todict_fromdict(object_type, objects):
+    """checks model objects in swh_model_data are in correct shape"""
+    assert objects
+    for obj in objects:
+        # Check the composition of from_dict and to_dict is the identity
+        obj_as_dict = obj.to_dict()
+        assert obj == type(obj).from_dict(obj_as_dict)
+        assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict()
+
+
 def test_unique_key():
     url = "http://example.org/"
     date = datetime.datetime.now(tz=datetime.timezone.utc)
-- 
GitLab