Skip to content

Commit 134641f

Browse files
committed
Made the dicts being serialized ordered.
Signed-off-by: KOLANICH <[email protected]>
1 parent 98587c5 commit 134641f

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

tuf/api/metadata.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ def __init__(
138138
if unrecognized_fields is None:
139139
unrecognized_fields = {}
140140

141-
self.unrecognized_fields = unrecognized_fields
141+
self.unrecognized_fields = type(unrecognized_fields)(
142+
sorted(unrecognized_fields.items(), key=lambda x: x[0])
143+
)
142144

143145
def __eq__(self, other: Any) -> bool:
144146
if not isinstance(other, Metadata):
@@ -293,7 +295,7 @@ def to_bytes(
293295

294296
return serializer.serialize(self)
295297

296-
def to_dict(self) -> Dict[str, Any]:
298+
def to_dict(self) -> Mapping[str, Any]:
297299
"""Returns the dict representation of self."""
298300

299301
signatures = [sig.to_dict() for sig in self.signatures.values()]
@@ -582,7 +584,7 @@ def _common_fields_from_dict(
582584

583585
return version, spec_version, expires
584586

585-
def _common_fields_to_dict(self) -> Dict[str, Any]:
587+
def _common_fields_to_dict(self) -> Mapping[str, Any]:
586588
"""Returns dict representation of common fields of ``Signed`` instances.
587589
588590
See ``{Root, Timestamp, Snapshot, Targets}.to_dict`` methods for usage.
@@ -682,7 +684,7 @@ def from_dict(cls, keyid: str, key_dict: Dict[str, Any]) -> "Key":
682684
# All fields left in the key_dict are unrecognized.
683685
return cls(keyid, keytype, scheme, keyval, key_dict)
684686

685-
def to_dict(self) -> Dict[str, Any]:
687+
def to_dict(self) -> Mapping[str, Any]:
686688
"""Returns the dictionary representation of self."""
687689
return {
688690
"keytype": self.keytype,
@@ -840,7 +842,7 @@ def from_dict(cls, role_dict: Dict[str, Any]) -> "Role":
840842
# All fields left in the role_dict are unrecognized.
841843
return cls(keyids, threshold, role_dict)
842844

843-
def to_dict(self) -> Dict[str, Any]:
845+
def to_dict(self) -> Mapping[str, Any]:
844846
"""Returns the dictionary representation of self."""
845847
return {
846848
"keyids": self.keyids,
@@ -927,12 +929,15 @@ def from_dict(cls, signed_dict: Dict[str, Any]) -> "Root":
927929
# All fields left in the signed_dict are unrecognized.
928930
return cls(*common_args, keys, roles, consistent_snapshot, signed_dict)
929931

930-
def to_dict(self) -> Dict[str, Any]:
932+
def to_dict(self) -> Mapping[str, Any]:
931933
"""Returns the dict representation of self."""
932934
root_dict = self._common_fields_to_dict()
933-
keys = {keyid: key.to_dict() for (keyid, key) in self.keys.items()}
935+
keys = {
936+
keyid: key.to_dict()
937+
for (keyid, key) in sorted(self.keys.items(), key=lambda x: x[0])
938+
}
934939
roles = {}
935-
for role_name, role in self.roles.items():
940+
for role_name, role in sorted(self.roles.items(), key=lambda x: x[0]):
936941
roles[role_name] = role.to_dict()
937942
if self.consistent_snapshot is not None:
938943
root_dict["consistent_snapshot"] = self.consistent_snapshot
@@ -1274,11 +1279,13 @@ def from_dict(cls, signed_dict: Dict[str, Any]) -> "Snapshot":
12741279
# All fields left in the snapshot_dict are unrecognized.
12751280
return cls(*common_args, meta, signed_dict)
12761281

1277-
def to_dict(self) -> Dict[str, Any]:
1282+
def to_dict(self) -> Mapping[str, Any]:
12781283
"""Returns the dict representation of self."""
12791284
snapshot_dict = self._common_fields_to_dict()
12801285
meta_dict = {}
1281-
for meta_path, meta_info in self.meta.items():
1286+
for meta_path, meta_info in sorted(
1287+
self.meta.items(), key=lambda x: x[0]
1288+
):
12821289
meta_dict[meta_path] = meta_info.to_dict()
12831290

12841291
snapshot_dict["meta"] = meta_dict
@@ -1695,9 +1702,12 @@ def from_dict(cls, delegations_dict: Dict[str, Any]) -> "Delegations":
16951702
# All fields left in the delegations_dict are unrecognized.
16961703
return cls(keys_res, roles_res, succinct_roles_info, delegations_dict)
16971704

1698-
def to_dict(self) -> Dict[str, Any]:
1705+
def to_dict(self) -> Mapping[str, Any]:
16991706
"""Returns the dict representation of self."""
1700-
keys = {keyid: key.to_dict() for keyid, key in self.keys.items()}
1707+
keys = {
1708+
keyid: key.to_dict()
1709+
for keyid, key in sorted(self.keys.items(), key=lambda x: x[0])
1710+
}
17011711
res_dict: Dict[str, Any] = {
17021712
"keys": keys,
17031713
**self.unrecognized_fields,
@@ -1967,11 +1977,13 @@ def from_dict(cls, signed_dict: Dict[str, Any]) -> "Targets":
19671977
# All fields left in the targets_dict are unrecognized.
19681978
return cls(*common_args, res_targets, delegations, signed_dict)
19691979

1970-
def to_dict(self) -> Dict[str, Any]:
1980+
def to_dict(self) -> Mapping[str, Any]:
19711981
"""Returns the dict representation of self."""
19721982
targets_dict = self._common_fields_to_dict()
19731983
targets = {}
1974-
for target_path, target_file_obj in self.targets.items():
1984+
for target_path, target_file_obj in sorted(
1985+
self.targets.items(), key=lambda x: x[0]
1986+
):
19751987
targets[target_path] = target_file_obj.to_dict()
19761988
targets_dict[_TARGETS] = targets
19771989
if self.delegations is not None:

0 commit comments

Comments
 (0)