@@ -320,14 +320,20 @@ class Signed:
320320 spec_version: The TUF specification version number (semver) the
321321 metadata format adheres to.
322322 expires: The metadata expiration datetime object.
323+ unrecognized_fields: Dictionary of all unrecognized fields.
323324
324325 """
325326
326327 # NOTE: Signed is a stupid name, because this might not be signed yet, but
327328 # we keep it to match spec terminology (I often refer to this as "payload",
328329 # or "inner metadata")
329330 def __init__ (
330- self , _type : str , version : int , spec_version : str , expires : datetime
331+ self ,
332+ _type : str ,
333+ version : int ,
334+ spec_version : str ,
335+ expires : datetime ,
336+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
331337 ) -> None :
332338
333339 self ._type = _type
@@ -338,6 +344,7 @@ def __init__(
338344 if version < 0 :
339345 raise ValueError (f"version must be >= 0, got { version } " )
340346 self .version = version
347+ self .unrecognized_fields = unrecognized_fields or {}
341348
342349 @staticmethod
343350 def _common_fields_from_dict (signed_dict : Mapping [str , Any ]) -> list :
@@ -369,6 +376,7 @@ def _common_fields_to_dict(self) -> Dict[str, Any]:
369376 "version" : self .version ,
370377 "spec_version" : self .spec_version ,
371378 "expires" : self .expires .isoformat () + "Z" ,
379+ ** self .unrecognized_fields ,
372380 }
373381
374382 def is_expired (self , reference_time : datetime = None ) -> bool :
@@ -445,8 +453,11 @@ def __init__(
445453 consistent_snapshot : bool ,
446454 keys : Mapping [str , Any ],
447455 roles : Mapping [str , Any ],
456+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
448457 ) -> None :
449- super ().__init__ (_type , version , spec_version , expires )
458+ super ().__init__ (
459+ _type , version , spec_version , expires , unrecognized_fields
460+ )
450461 # TODO: Add classes for keys and roles
451462 self .consistent_snapshot = consistent_snapshot
452463 self .keys = keys
@@ -459,7 +470,8 @@ def from_dict(cls, root_dict: Mapping[str, Any]) -> "Root":
459470 consistent_snapshot = root_dict .pop ("consistent_snapshot" )
460471 keys = root_dict .pop ("keys" )
461472 roles = root_dict .pop ("roles" )
462- return cls (* common_args , consistent_snapshot , keys , roles )
473+ # All fields left in the root_dict are unrecognized.
474+ return cls (* common_args , consistent_snapshot , keys , roles , root_dict )
463475
464476 def to_dict (self ) -> Dict [str , Any ]:
465477 """Returns the dict representation of self. """
@@ -521,8 +533,11 @@ def __init__(
521533 spec_version : str ,
522534 expires : datetime ,
523535 meta : Mapping [str , Any ],
536+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
524537 ) -> None :
525- super ().__init__ (_type , version , spec_version , expires )
538+ super ().__init__ (
539+ _type , version , spec_version , expires , unrecognized_fields
540+ )
526541 # TODO: Add class for meta
527542 self .meta = meta
528543
@@ -531,7 +546,8 @@ def from_dict(cls, timestamp_dict: Mapping[str, Any]) -> "Timestamp":
531546 """Creates Timestamp object from its dict representation. """
532547 common_args = cls ._common_fields_from_dict (timestamp_dict )
533548 meta = timestamp_dict .pop ("meta" )
534- return cls (* common_args , meta )
549+ # All fields left in the timestamp_dict are unrecognized.
550+ return cls (* common_args , meta , timestamp_dict )
535551
536552 def to_dict (self ) -> Dict [str , Any ]:
537553 """Returns the dict representation of self. """
@@ -585,8 +601,11 @@ def __init__(
585601 spec_version : str ,
586602 expires : datetime ,
587603 meta : Mapping [str , Any ],
604+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
588605 ) -> None :
589- super ().__init__ (_type , version , spec_version , expires )
606+ super ().__init__ (
607+ _type , version , spec_version , expires , unrecognized_fields
608+ )
590609 # TODO: Add class for meta
591610 self .meta = meta
592611
@@ -595,7 +614,8 @@ def from_dict(cls, snapshot_dict: Mapping[str, Any]) -> "Snapshot":
595614 """Creates Snapshot object from its dict representation. """
596615 common_args = cls ._common_fields_from_dict (snapshot_dict )
597616 meta = snapshot_dict .pop ("meta" )
598- return cls (* common_args , meta )
617+ # All fields left in the snapshot_dict are unrecognized.
618+ return cls (* common_args , meta , snapshot_dict )
599619
600620 def to_dict (self ) -> Dict [str , Any ]:
601621 """Returns the dict representation of self. """
@@ -688,8 +708,11 @@ def __init__(
688708 expires : datetime ,
689709 targets : Mapping [str , Any ],
690710 delegations : Mapping [str , Any ],
711+ unrecognized_fields : Optional [Mapping [str , Any ]] = None ,
691712 ) -> None :
692- super ().__init__ (_type , version , spec_version , expires )
713+ super ().__init__ (
714+ _type , version , spec_version , expires , unrecognized_fields
715+ )
693716 # TODO: Add class for meta
694717 self .targets = targets
695718 self .delegations = delegations
@@ -700,7 +723,8 @@ def from_dict(cls, targets_dict: Mapping[str, Any]) -> "Targets":
700723 common_args = cls ._common_fields_from_dict (targets_dict )
701724 targets = targets_dict .pop ("targets" )
702725 delegations = targets_dict .pop ("delegations" )
703- return cls (* common_args , targets , delegations )
726+ # All fields left in the targets_dict are unrecognized.
727+ return cls (* common_args , targets , delegations , targets_dict )
704728
705729 def to_dict (self ) -> Dict [str , Any ]:
706730 """Returns the dict representation of self. """
0 commit comments