Skip to content

Commit 89ee462

Browse files
committed
BREAKING CHANGE: Make delegations optional
According to the spec, delegations in targets are marked as optional: https://theupdateframework.github.io/specification/latest/#file-formats-targets and a pr, clarifying that even more, is approved: theupdateframework/specification#157. This is a possible breaking change. Signed-off-by: Martin Vrachev <[email protected]>
1 parent b6b5aac commit 89ee462

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

tests/test_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ def test_metadata_targets(self):
445445
# Verify that data is updated
446446
self.assertEqual(targets.signed.targets[filename], fileinfo)
447447

448+
# Test from_dict/to_dict Targets without delegations
449+
targets_dict = targets.to_dict()
450+
del targets_dict["signed"]["delegations"]
451+
tmp_dict = targets_dict["signed"].copy()
452+
targets_obj = Targets.from_dict(tmp_dict)
453+
tar_d = targets_obj.to_dict()
454+
self.assertEqual(targets_dict["signed"], targets_obj.to_dict())
455+
448456
def setup_dict_with_unrecognized_field(self, file_path, field, value):
449457
json_dict = {}
450458
with open(file_path) as f:

tuf/api/metadata.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ def __init__(
817817
spec_version: str,
818818
expires: datetime,
819819
targets: Mapping[str, Any],
820-
delegations: Mapping[str, Any],
820+
delegations: Optional[Mapping[str, Any]] = None,
821821
unrecognized_fields: Optional[Mapping[str, Any]] = None,
822822
) -> None:
823823
super().__init__(
@@ -832,19 +832,16 @@ def from_dict(cls, targets_dict: Mapping[str, Any]) -> "Targets":
832832
"""Creates Targets object from its dict representation."""
833833
common_args = cls._common_fields_from_dict(targets_dict)
834834
targets = targets_dict.pop("targets")
835-
delegations = targets_dict.pop("delegations")
835+
delegations = targets_dict.pop("delegations", None)
836836
# All fields left in the targets_dict are unrecognized.
837837
return cls(*common_args, targets, delegations, targets_dict)
838838

839839
def to_dict(self) -> Dict[str, Any]:
840840
"""Returns the dict representation of self."""
841841
targets_dict = self._common_fields_to_dict()
842-
targets_dict.update(
843-
{
844-
"targets": self.targets,
845-
"delegations": self.delegations,
846-
}
847-
)
842+
targets_dict["targets"] = self.targets
843+
if self.delegations:
844+
targets_dict["delegations"] = self.delegations
848845
return targets_dict
849846

850847
# Modification.

0 commit comments

Comments
 (0)