diff --git a/swh/model/model.py b/swh/model/model.py --- a/swh/model/model.py +++ b/swh/model/model.py @@ -1564,6 +1564,12 @@ @classmethod def from_dict(cls, d): + if "type" in d: + # Convert from old schema + type_ = d.pop("type") + if type_ == "origin": + d["target"] = str(Origin(d["target"]).swhid()) + d = { **d, "target": ExtendedSWHID.from_string(d["target"]), diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -1428,6 +1428,41 @@ ) +def test_metadata_from_old_dict(): + common_fields = { + "authority": {"type": "forge", "url": "https://forge.softwareheritage.org"}, + "fetcher": { + "name": "test-fetcher", + "version": "0.0.1", + }, + "discovery_date": _common_metadata_fields["discovery_date"], + "format": "json", + "metadata": b'{"origin": "https://example.com", "lines": "42"}', + } + + m = RawExtrinsicMetadata( + target=_origin_swhid, + **common_fields, + ) + assert ( + RawExtrinsicMetadata.from_dict( + {"target": _origin_url, "type": "origin", **common_fields} + ) + == m + ) + + m = RawExtrinsicMetadata( + target=_origin_swhid, + **common_fields, + ) + assert ( + RawExtrinsicMetadata.from_dict( + {"target": _content_swhid, "type": "content", **common_fields} + ) + == m + ) + + def test_metadata_to_dict(): """Checks valid RawExtrinsicMetadata objects don't raise an error."""