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>
This commit is contained in:
Jussi Kukkonen 2021-05-14 14:02:29 +03:00
parent 0bbfe038cf
commit 112b333bba
2 changed files with 13 additions and 11 deletions

View file

@ -450,21 +450,23 @@ def test_delegated_role_class(self):
with self.assertRaises(ValueError):
DelegatedRole.from_dict(role.copy())
# Test creating DelegatedRole only with "path_hash_prefixes"
# Test creating DelegatedRole only with "path_hash_prefixes" (an empty one)
del role["paths"]
DelegatedRole.from_dict(role.copy())
role["paths"] = "foo"
role["path_hash_prefixes"] = []
role_obj = DelegatedRole.from_dict(role.copy())
self.assertEqual(role_obj.to_dict(), role)
# Test creating DelegatedRole only with "paths"
# Test creating DelegatedRole only with "paths" (now an empty one)
del role["path_hash_prefixes"]
DelegatedRole.from_dict(role.copy())
role["path_hash_prefixes"] = "foo"
role["paths"] = []
role_obj = DelegatedRole.from_dict(role.copy())
self.assertEqual(role_obj.to_dict(), role)
# Test creating DelegatedRole without "paths" and
# "path_hash_prefixes" set
del role["paths"]
del role["path_hash_prefixes"]
DelegatedRole.from_dict(role)
role_obj = DelegatedRole.from_dict(role.copy())
self.assertEqual(role_obj.to_dict(), role)
def test_delegation_class(self):

View file

@ -770,7 +770,7 @@ def __init__(
super().__init__(keyids, threshold, unrecognized_fields)
self.name = name
self.terminating = terminating
if paths and path_hash_prefixes:
if paths is not None and path_hash_prefixes is not None:
raise ValueError(
"Only one of the attributes 'paths' and"
"'path_hash_prefixes' can be set!"
@ -806,9 +806,9 @@ def to_dict(self) -> Dict[str, Any]:
"terminating": self.terminating,
**base_role_dict,
}
if self.paths:
if self.paths is not None:
res_dict["paths"] = self.paths
elif self.path_hash_prefixes:
elif self.path_hash_prefixes is not None:
res_dict["path_hash_prefixes"] = self.path_hash_prefixes
return res_dict