Skip to content

Commit

Permalink
BREAKING CHANGE: Make delegations optional
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
MVrachev committed May 10, 2021
1 parent 0604f70 commit 139bfc0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
8 changes: 8 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ def test_metadata_targets(self):
# Verify that data is updated
self.assertEqual(targets.signed.targets[filename], fileinfo)

# Test from_dict/to_dict Targets without delegations
targets_dict = targets.to_dict()
del targets_dict["signed"]["delegations"]
tmp_dict = targets_dict["signed"].copy()
targets_obj = Targets.from_dict(tmp_dict)
tar_d = targets_obj.to_dict()
self.assertEqual(targets_dict["signed"], targets_obj.to_dict())

def setup_dict_with_unrecognized_field(self, file_path, field, value):
json_dict = {}
with open(file_path) as f:
Expand Down
13 changes: 5 additions & 8 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ def __init__(
spec_version: str,
expires: datetime,
targets: Mapping[str, Any],
delegations: Mapping[str, Any],
delegations: Optional[Mapping[str, Any]] = None,
unrecognized_fields: Optional[Mapping[str, Any]] = None,
) -> None:
super().__init__(version, spec_version, expires, unrecognized_fields)
Expand All @@ -819,19 +819,16 @@ def from_dict(cls, targets_dict: Mapping[str, Any]) -> "Targets":
"""Creates Targets object from its dict representation."""
common_args = cls._common_fields_from_dict(targets_dict)
targets = targets_dict.pop("targets")
delegations = targets_dict.pop("delegations")
delegations = targets_dict.pop("delegations", None)
# All fields left in the targets_dict are unrecognized.
return cls(*common_args, targets, delegations, targets_dict)

def to_dict(self) -> Dict[str, Any]:
"""Returns the dict representation of self."""
targets_dict = self._common_fields_to_dict()
targets_dict.update(
{
"targets": self.targets,
"delegations": self.delegations,
}
)
targets_dict["targets"] = self.targets
if self.delegations:
targets_dict["delegations"] = self.delegations
return targets_dict

# Modification.
Expand Down

0 comments on commit 139bfc0

Please sign in to comment.