Skip to content

Commit aa480b1

Browse files
author
Jussi Kukkonen
committed
Metadata API: Fix DelegatedRole serialization issue
A DelegatedRole with paths=[] fails to serialize correctly (paths is not included in the output json). Fix the issue, modify tests to notice a regression. Fixes #1389 Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
1 parent 9a397b9 commit aa480b1

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

tests/test_api.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,21 +451,23 @@ def test_delegated_role_class(self):
451451
with self.assertRaises(ValueError):
452452
DelegatedRole.from_dict(role.copy())
453453

454-
# Test creating DelegatedRole only with "path_hash_prefixes"
454+
# Test creating DelegatedRole only with "path_hash_prefixes" (an empty one)
455455
del role["paths"]
456-
DelegatedRole.from_dict(role.copy())
457-
role["paths"] = "foo"
456+
role["path_hash_prefixes"] = []
457+
role_obj = DelegatedRole.from_dict(role.copy())
458+
self.assertEqual(role_obj.to_dict(), role)
458459

459-
# Test creating DelegatedRole only with "paths"
460+
# Test creating DelegatedRole only with "paths" (now an empty one)
460461
del role["path_hash_prefixes"]
461-
DelegatedRole.from_dict(role.copy())
462-
role["path_hash_prefixes"] = "foo"
462+
role["paths"] = []
463+
role_obj = DelegatedRole.from_dict(role.copy())
464+
self.assertEqual(role_obj.to_dict(), role)
463465

464466
# Test creating DelegatedRole without "paths" and
465467
# "path_hash_prefixes" set
466468
del role["paths"]
467-
del role["path_hash_prefixes"]
468-
DelegatedRole.from_dict(role)
469+
role_obj = DelegatedRole.from_dict(role.copy())
470+
self.assertEqual(role_obj.to_dict(), role)
469471

470472

471473
def test_delegation_class(self):

tuf/api/metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def __init__(
768768
super().__init__(keyids, threshold, unrecognized_fields)
769769
self.name = name
770770
self.terminating = terminating
771-
if paths and path_hash_prefixes:
771+
if paths is not None and path_hash_prefixes is not None:
772772
raise ValueError(
773773
"Only one of the attributes 'paths' and"
774774
"'path_hash_prefixes' can be set!"
@@ -804,9 +804,9 @@ def to_dict(self) -> Dict[str, Any]:
804804
"terminating": self.terminating,
805805
**base_role_dict,
806806
}
807-
if self.paths:
807+
if self.paths is not None:
808808
res_dict["paths"] = self.paths
809-
elif self.path_hash_prefixes:
809+
elif self.path_hash_prefixes is not None:
810810
res_dict["path_hash_prefixes"] = self.path_hash_prefixes
811811
return res_dict
812812

0 commit comments

Comments
 (0)