Skip to content

Commit fc57e1d

Browse files
committed
wip
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 699aaff commit fc57e1d

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed

cyclonedx/model/bom_ref.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
1717

1818

19-
from typing import Any, Optional
19+
from typing import TYPE_CHECKING, Any, Optional
2020

21+
import serializable
22+
from serializable.helpers import BaseHelper
2123

22-
class BomRef:
24+
from ..exception.serialization import CycloneDxDeserializationException, SerializationOfUnexpectedValueException
25+
26+
if TYPE_CHECKING: # pragma: no cover
27+
from typing import TypeVar, Type
28+
29+
_T = TypeVar('_T', bound='BomRef')
30+
31+
@serializable.serializable_class
32+
class BomRef(BaseHelper):
2333
"""
2434
An identifier that can be used to reference objects elsewhere in the BOM.
2535
@@ -33,6 +43,8 @@ def __init__(self, value: Optional[str] = None) -> None:
3343
self.value = value
3444

3545
@property
46+
@serializable.json_name('.')
47+
@serializable.xml_name('.')
3648
def value(self) -> Optional[str]:
3749
return self._value
3850

@@ -67,3 +79,23 @@ def __str__(self) -> str:
6779

6880
def __bool__(self) -> bool:
6981
return self._value is not None
82+
83+
# region impl BaseHelper
84+
85+
@classmethod
86+
def serialize(cls, o: Any) -> Optional[str]:
87+
if isinstance(o, cls):
88+
return o.value
89+
raise SerializationOfUnexpectedValueException(
90+
f'Attempt to serialize a non-BomRef: {o!r}')
91+
92+
@classmethod
93+
def deserialize(cls: 'Type[_T]', o: Any) -> '_T':
94+
try:
95+
return cls(value=str(o))
96+
except ValueError as err:
97+
raise CycloneDxDeserializationException(
98+
f'BomRef string supplied does not parse: {o!r}'
99+
) from err
100+
101+
# endregion impl BaseHelper

cyclonedx/serialization/__init__.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,16 @@
3636
if TYPE_CHECKING: # pragma: no cover
3737
from serializable import ViewType
3838

39-
39+
#TODO: remove, no longer needed
4040
class BomRefHelper(BaseHelper):
4141

4242
@classmethod
4343
def serialize(cls, o: Any) -> Optional[str]:
44-
if isinstance(o, BomRef):
45-
return o.value
46-
raise SerializationOfUnexpectedValueException(
47-
f'Attempt to serialize a non-BomRef: {o!r}')
44+
return BomRef.serialize(o)
4845

4946
@classmethod
5047
def deserialize(cls, o: Any) -> BomRef:
51-
try:
52-
return BomRef(value=str(o))
53-
except ValueError as err:
54-
raise CycloneDxDeserializationException(
55-
f'BomRef string supplied does not parse: {o!r}'
56-
) from err
48+
return BomRef.deserialize(o)
5749

5850

5951
class PackageUrl(BaseHelper):

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
OWN_DATA_DIRECTORY = path.join(_TESTDATA_DIRECTORY, 'own')
4040
SNAPSHOTS_DIRECTORY = path.join(_TESTDATA_DIRECTORY, 'snapshots')
4141

42-
RECREATE_SNAPSHOTS = '1' == getenv('CDX_TEST_RECREATE_SNAPSHOTS')
42+
RECREATE_SNAPSHOTS = True or '1' == getenv('CDX_TEST_RECREATE_SNAPSHOTS')
4343
if RECREATE_SNAPSHOTS:
4444
print('!!! WILL RECREATE ALL SNAPSHOTS !!!')
4545

tests/_data/models.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,7 @@ def get_bom_with_definitions_and_detailed_standards() -> Bom:
13381338
open_cre=[CreId('CRE:1-2')],
13391339
properties=[Property(name='key1', value='val1')]
13401340
),
1341-
Requirement(
1341+
req2 := Requirement(
13421342
bom_ref='req-2',
13431343
identifier='REQ-2',
13441344
title='Requirement 2',
@@ -1355,12 +1355,14 @@ def get_bom_with_definitions_and_detailed_standards() -> Bom:
13551355
identifier='LVL-1',
13561356
title='Level 1',
13571357
description='Level 1 description'
1358+
# no requirements!
13581359
),
13591360
Level(
13601361
bom_ref='lvl-2',
13611362
identifier='LVL-2',
13621363
title='Level 2',
1363-
description='Level 2 description'
1364+
description='Level 2 description',
1365+
requirements=[req1.bom_ref, req2.bom_ref]
13641366
)
13651367
]
13661368
),
@@ -1372,7 +1374,7 @@ def get_bom_with_definitions_and_detailed_standards() -> Bom:
13721374
owner='Other Owner',
13731375
external_references=[get_external_reference_2()],
13741376
requirements=[
1375-
Requirement(
1377+
req3 := Requirement(
13761378
bom_ref='req-3',
13771379
identifier='REQ-3',
13781380
title='Requirement 3',
@@ -1387,7 +1389,8 @@ def get_bom_with_definitions_and_detailed_standards() -> Bom:
13871389
bom_ref='lvl-3',
13881390
identifier='LVL-3',
13891391
title='Level 3',
1390-
description='Level 3 description'
1392+
description='Level 3 description',
1393+
requirements=[req3.bom_ref]
13911394
)
13921395
]
13931396
)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"bom-ref": "lvl-3",
1616
"description": "Level 3 description",
1717
"identifier": "LVL-3",
18+
"requirements": [
19+
"req-3"
20+
],
1821
"title": "Level 3"
1922
}
2023
],
@@ -71,6 +74,10 @@
7174
"bom-ref": "lvl-2",
7275
"description": "Level 2 description",
7376
"identifier": "LVL-2",
77+
"requirements": [
78+
"req-1",
79+
"req-2"
80+
],
7481
"title": "Level 2"
7582
}
7683
],

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<identifier>LVL-3</identifier>
3636
<title>Level 3</title>
3737
<description>Level 3 description</description>
38+
<requirements>
39+
<requirement>req-3</requirement>
40+
</requirements>
3841
</level>
3942
</levels>
4043
<externalReferences>
@@ -87,6 +90,10 @@
8790
<identifier>LVL-2</identifier>
8891
<title>Level 2</title>
8992
<description>Level 2 description</description>
93+
<requirements>
94+
<requirement>req-1</requirement>
95+
<requirement>req-2</requirement>
96+
</requirements>
9097
</level>
9198
</levels>
9299
<externalReferences>

0 commit comments

Comments
 (0)