Skip to content

Commit 47a1ef4

Browse files
committed
clean _IdentityRepositorySerializationHelper
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent b66ecc3 commit 47a1ef4

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

cyclonedx/model/component_evidence.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -623,31 +623,29 @@ def __repr__(self) -> str:
623623
return f'<CallStack frames={len(self.frames)}>'
624624

625625

626-
class _IdentitySerializationHelper(serializable.helpers.BaseHelper):
626+
class _IdentityRepositorySerializationHelper(serializable.helpers.BaseHelper):
627627
"""THIS CLASS IS NON-PUBLIC API"""
628628

629629
@classmethod
630630
def json_normalize(cls, o: SortedSet[Identity], *,
631631
view: Optional[type[serializable.ViewType]],
632-
**__: Any) -> Any:
632+
**__: Any) -> Union[dict,list[dict],None]:
633633
if not o:
634634
return None
635-
636-
# For Schema 1.5 JSON, return first identity as a single object
637-
if view and issubclass(view, SchemaVersion1Dot5):
638-
first_identity = next(iter(o))
635+
if view and view is SchemaVersion1Dot5:
636+
# For Schema 1.5 JSON, return first identity as a single object
637+
first_identity = o[0]
639638
return json_loads(first_identity.as_json(view_=view)) # type: ignore[attr-defined]
640-
641639
# For Schema 1.6 and others, return array of all identities
642640
return [json_loads(identity.as_json(view_=view)) for identity in o] # type: ignore[attr-defined]
643641

644642
@classmethod
645-
def json_denormalize(cls, o: Any, **__: Any) -> SortedSet[Identity]:
643+
def json_denormalize(cls, o: Any, **__: Any) -> Optional[list[Identity]]:
646644
if isinstance(o, dict): # Single Identity object (Schema 1.5)
647-
return SortedSet([Identity.from_json(o)]) # type: ignore[attr-defined]
645+
return [Identity.from_json(o)] # type: ignore[attr-defined]
648646
elif isinstance(o, (list, tuple)): # Array of Identity objects (Schema 1.6)
649-
return SortedSet(Identity.from_json(i) for i in o) # type: ignore[attr-defined]
650-
return SortedSet()
647+
return [Identity.from_json(i) for i in o] # type: ignore[attr-defined]
648+
return None
651649

652650

653651
@serializable.serializable_class
@@ -679,7 +677,7 @@ def __init__(
679677
@serializable.view(SchemaVersion1Dot5)
680678
@serializable.view(SchemaVersion1Dot6)
681679
@serializable.xml_sequence(1)
682-
@serializable.type_mapping(_IdentitySerializationHelper)
680+
@serializable.type_mapping(_IdentityRepositorySerializationHelper)
683681
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'identity')
684682
# TODO: CDX 1.5 knows only one identity, all versions later known multiple ...
685683
# TODO: need to fix the serialization/normalization

tests/_data/snapshots/get_bom_with_component_evidence-1.6.json.bin

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@
4343
"tools": [
4444
"cbom:generator"
4545
]
46+
},
47+
{
48+
"concludedValue": "example-component",
49+
"confidence": 0.9,
50+
"field": "name",
51+
"methods": [
52+
{
53+
"confidence": 0.8,
54+
"technique": "source-code-analysis",
55+
"value": "analysis-tool"
56+
}
57+
],
58+
"tools": [
59+
"cbom:generator"
60+
]
4661
}
4762
],
4863
"licenses": [

tests/_data/snapshots/get_bom_with_component_evidence-1.6.xml.bin

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@
4545
<tool ref="cbom:generator"/>
4646
</tools>
4747
</identity>
48+
<identity>
49+
<field>name</field>
50+
<confidence>0.9</confidence>
51+
<concludedValue>example-component</concludedValue>
52+
<methods>
53+
<method>
54+
<technique>source-code-analysis</technique>
55+
<confidence>0.8</confidence>
56+
<value>analysis-tool</value>
57+
</method>
58+
</methods>
59+
<tools>
60+
<tool ref="cbom:generator"/>
61+
</tools>
62+
</identity>
4863
<occurrences>
4964
<occurrence>
5065
<location>path/to/file</location>

0 commit comments

Comments
 (0)