Skip to content

Commit 19cb525

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 376d394 commit 19cb525

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
@@ -375,6 +375,14 @@ def test_metadata_targets(self):
375375
# Verify that data is updated
376376
self.assertEqual(targets.signed.targets[filename], fileinfo)
377377

378+
# Test from_dict/to_dict Targets without delegations
379+
targets_dict = targets.to_dict()
380+
del targets_dict["signed"]["delegations"]
381+
tmp_dict = targets_dict["signed"].copy()
382+
targets_obj = Targets.from_dict(tmp_dict)
383+
tar_d = targets_obj.to_dict()
384+
self.assertEqual(targets_dict["signed"], targets_obj.to_dict())
385+
378386
def setup_dict_with_unrecognized_field(self, file_path, field, value):
379387
json_dict = {}
380388
with open(file_path) as f:

tuf/api/metadata.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ def __init__(
717717
spec_version: str,
718718
expires: datetime,
719719
targets: Mapping[str, Any],
720-
delegations: Mapping[str, Any],
720+
delegations: Optional[Mapping[str, Any]] = None,
721721
unrecognized_fields: Optional[Mapping[str, Any]] = None,
722722
) -> None:
723723
super().__init__(
@@ -732,19 +732,16 @@ def from_dict(cls, targets_dict: Mapping[str, Any]) -> "Targets":
732732
"""Creates Targets object from its dict representation."""
733733
common_args = cls._common_fields_from_dict(targets_dict)
734734
targets = targets_dict.pop("targets")
735-
delegations = targets_dict.pop("delegations")
735+
delegations = targets_dict.pop("delegations", None)
736736
# All fields left in the targets_dict are unrecognized.
737737
return cls(*common_args, targets, delegations, targets_dict)
738738

739739
def to_dict(self) -> Dict[str, Any]:
740740
"""Returns the dict representation of self."""
741741
targets_dict = self._common_fields_to_dict()
742-
targets_dict.update(
743-
{
744-
"targets": self.targets,
745-
"delegations": self.delegations,
746-
}
747-
)
742+
targets_dict["targets"] = self.targets
743+
if self.delegations:
744+
targets_dict["delegations"] = self.delegations
748745
return targets_dict
749746

750747
# Modification.

0 commit comments

Comments
 (0)