Skip to content

Commit 139bfc0

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 <mvrachev@vmware.com>
1 parent 0604f70 commit 139bfc0

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
@@ -806,7 +806,7 @@ def __init__(
806806
spec_version: str,
807807
expires: datetime,
808808
targets: Mapping[str, Any],
809-
delegations: Mapping[str, Any],
809+
delegations: Optional[Mapping[str, Any]] = None,
810810
unrecognized_fields: Optional[Mapping[str, Any]] = None,
811811
) -> None:
812812
super().__init__(version, spec_version, expires, unrecognized_fields)
@@ -819,19 +819,16 @@ def from_dict(cls, targets_dict: Mapping[str, Any]) -> "Targets":
819819
"""Creates Targets object from its dict representation."""
820820
common_args = cls._common_fields_from_dict(targets_dict)
821821
targets = targets_dict.pop("targets")
822-
delegations = targets_dict.pop("delegations")
822+
delegations = targets_dict.pop("delegations", None)
823823
# All fields left in the targets_dict are unrecognized.
824824
return cls(*common_args, targets, delegations, targets_dict)
825825

826826
def to_dict(self) -> Dict[str, Any]:
827827
"""Returns the dict representation of self."""
828828
targets_dict = self._common_fields_to_dict()
829-
targets_dict.update(
830-
{
831-
"targets": self.targets,
832-
"delegations": self.delegations,
833-
}
834-
)
829+
targets_dict["targets"] = self.targets
830+
if self.delegations:
831+
targets_dict["delegations"] = self.delegations
835832
return targets_dict
836833

837834
# Modification.

0 commit comments

Comments
 (0)