Skip to content

Commit 054334b

Browse files
committed
wip
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 0f17348 commit 054334b

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

cyclonedx/model/component_evidence.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ def __hash__(self) -> int:
288288

289289
def __repr__(self) -> str:
290290
return f'<Identity field={self.field}, confidence={self.confidence},' \
291-
f' concludedValue={self.concluded_value},' \
292-
f' methods={self.methods}, tools={self.tools}>'
291+
f' concludedValue={self.concluded_value},' \
292+
f' methods={self.methods}, tools={self.tools}>'
293293

294294

295295
@serializable.serializable_class(ignore_unknown_during_deserialization=True)
@@ -768,6 +768,10 @@ def json_normalize(cls, o: ComponentEvidence, *,
768768

769769
@classmethod
770770
def json_denormalize(cls, o: dict[str, Any], **__: Any) -> Any:
771+
if isinstance(identity := o.get('identity', []), dict):
772+
# Handle identity field which can be a dict (CycloneDX 1.5) or list of dicts (CycloneDX 1.6)
773+
# Before passing to ComponentEvidence.from_json, ensure it's always a list
774+
o = {**o, 'identity': [identity]}
771775
return ComponentEvidence.from_json(o) # type:ignore[attr-defined]
772776

773777
@classmethod

tests/_data/own/json/1.6/component_evidence_identity.json

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_deserialize_json.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,13 @@ def test_regression_issue690(self) -> None:
127127
json = json_loads(f.read())
128128
bom: Bom = Bom.from_json(json) # <<< is expected to not crash
129129
self.assertIsNotNone(bom)
130+
131+
def test_component_evidence_identity(self) -> None:
132+
"""Since 1.8 it is allowed to have component evidence identity as a list or an object"""
133+
json_file = join(OWN_DATA_DIRECTORY, 'json',
134+
SchemaVersion.V1_6.to_version(),
135+
'component_evidence_identity.json')
136+
with open(json_file) as f:
137+
json = json_loads(f.read())
138+
bom: Bom = Bom.from_json(json) # <<< is expected to not crash
139+
self.assertIsNotNone(bom)

tests/test_model_component_evidence.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class TestModelComponentEvidence(TestCase):
3737
def test_no_params(self) -> None:
3838
ComponentEvidence() # Does not raise `NoPropertiesProvidedException`
3939

40-
def test_identity(self) -> None:
40+
def test_identity_single(self) -> None:
4141
identity = Identity(field=IdentityField.NAME, confidence=Decimal('1'), concluded_value='test')
42-
ce = ComponentEvidence(identity=[identity])
42+
ce = ComponentEvidence(identity=identity)
4343
self.assertEqual(len(ce.identity), 1)
4444
self.assertEqual(ce.identity.pop().field, 'name')
4545

@@ -201,7 +201,6 @@ def test_not_same_1(self) -> None:
201201
self.assertNotEqual(hash(ce_1), hash(ce_2))
202202
self.assertFalse(ce_1 == ce_2)
203203

204-
205204
class TestModelCallStackFrame(TestCase):
206205

207206
def test_fields(self) -> None:

0 commit comments

Comments
 (0)