Skip to content

Commit 46bc3f5

Browse files
jkowalleckwkoot
andauthored
feat!: BomRef affect equality/comparisson (#754)
For some this is considered a bug-fix, for others this is a feature - it is a breaking change anyway since it modifies the order of things. ---- TODO: - [x] **every** symbol that has a property `bom-ref` MUST utilize it for dunder methods `hash`,`eq`,`gt`,`lt`,... - [x] add new test cases from #753 - [x] add new test cases from #540 - [x] add new test cases from #677 - [x] create new tests snapshots (if applicable) ---- > [!important] > depends on #755 supersedes #678 closes #678 fixes #753 fixes #540 fixes #677 --------- Signed-off-by: wkoot <[email protected]> Signed-off-by: Jan Kowalleck <[email protected]> Co-authored-by: wkoot <[email protected]>
1 parent 6350438 commit 46bc3f5

File tree

35 files changed

+719
-75
lines changed

35 files changed

+719
-75
lines changed

cyclonedx/model/component.py

+1
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,7 @@ def get_pypi_url(self) -> str:
17741774
def __comparable_tuple(self) -> _ComparableTuple:
17751775
return _ComparableTuple((
17761776
self.type, self.group, self.name, self.version,
1777+
self.bom_ref.value,
17771778
None if self.purl is None else _ComparablePackageURL(self.purl),
17781779
self.swid, self.cpe, _ComparableTuple(self.swhids),
17791780
self.supplier, self.author, self.publisher,

cyclonedx/model/contact.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ def street_address(self, street_address: Optional[str]) -> None:
163163

164164
def __comparable_tuple(self) -> _ComparableTuple:
165165
return _ComparableTuple((
166-
self.bom_ref,
167166
self.country, self.region, self.locality, self.postal_code,
168167
self.post_office_box_number,
169-
self.street_address
168+
self.street_address,
169+
None if self.bom_ref is None else self.bom_ref.value,
170170
))
171171

172172
def __eq__(self, other: object) -> bool:

cyclonedx/model/definition.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def external_references(self, external_references: Iterable[ExternalReference])
256256
def __comparable_tuple(self) -> _ComparableTuple:
257257
# all properties are optional - so need to compare all, in hope that one is unique
258258
return _ComparableTuple((
259-
self.bom_ref, self.identifier,
259+
self.identifier, self.bom_ref.value,
260260
self.title, self.text,
261261
_ComparableTuple(self.descriptions),
262262
_ComparableTuple(self.open_cre), self.parent, _ComparableTuple(self.properties),
@@ -373,7 +373,9 @@ def requirements(self, requirements: Iterable[Union[str, BomRef]]) -> None:
373373
def __comparable_tuple(self) -> _ComparableTuple:
374374
# all properties are optional - so need to compare all, in hope that one is unique
375375
return _ComparableTuple((
376-
self.bom_ref, self.identifier, self.title, self.description, _ComparableTuple(self.requirements)
376+
self.identifier, self.bom_ref.value,
377+
self.title, self.description,
378+
_ComparableTuple(self.requirements)
377379
))
378380

379381
def __lt__(self, other: Any) -> bool:
@@ -545,8 +547,9 @@ def external_references(self, external_references: Iterable[ExternalReference])
545547
def __comparable_tuple(self) -> _ComparableTuple:
546548
# all properties are optional - so need to apply all, in hope that one is unique
547549
return _ComparableTuple((
548-
self.bom_ref,
549-
self.name, self.version, self.description, self.owner,
550+
self.name, self.version,
551+
self.bom_ref.value,
552+
self.description, self.owner,
550553
_ComparableTuple(self.requirements), _ComparableTuple(self.levels),
551554
_ComparableTuple(self.external_references)
552555
))

cyclonedx/model/service.py

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ def release_notes(self, release_notes: Optional[ReleaseNotes]) -> None:
355355
def __comparable_tuple(self) -> _ComparableTuple:
356356
return _ComparableTuple((
357357
self.group, self.name, self.version,
358+
self.bom_ref.value,
358359
self.provider, self.description,
359360
self.authenticated, _ComparableTuple(self.data), _ComparableTuple(self.endpoints),
360361
_ComparableTuple(self.external_references), _ComparableTuple(self.licenses),

cyclonedx/model/vulnerability.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ def properties(self, properties: Iterable[Property]) -> None:
13341334

13351335
def __comparable_tuple(self) -> _ComparableTuple:
13361336
return _ComparableTuple((
1337-
self.id,
1337+
self.id, self.bom_ref.value,
13381338
self.source, _ComparableTuple(self.references),
13391339
_ComparableTuple(self.ratings), _ComparableTuple(self.cwes), self.description,
13401340
self.detail, self.recommendation, self.workaround, _ComparableTuple(self.advisories),

tests/_data/models.py

+31
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,37 @@ def get_bom_with_definitions_and_detailed_standards() -> Bom:
14011401
]))
14021402

14031403

1404+
def get_bom_for_issue540_duplicate_components() -> Bom:
1405+
# tests https://github.com/CycloneDX/cyclonedx-python-lib/issues/540
1406+
bom = _make_bom()
1407+
bom.metadata.component = root_component = Component(
1408+
name='myApp',
1409+
type=ComponentType.APPLICATION,
1410+
bom_ref='myApp'
1411+
)
1412+
component1 = Component(
1413+
type=ComponentType.LIBRARY,
1414+
name='some-component',
1415+
bom_ref='some-component'
1416+
)
1417+
bom.components.add(component1)
1418+
bom.register_dependency(root_component, [component1])
1419+
component2 = Component(
1420+
type=ComponentType.LIBRARY,
1421+
name='some-library',
1422+
bom_ref='some-library1'
1423+
)
1424+
bom.components.add(component2)
1425+
bom.register_dependency(component1, [component2])
1426+
component3 = Component(
1427+
type=ComponentType.LIBRARY,
1428+
name='some-library',
1429+
bom_ref='some-library2'
1430+
)
1431+
bom.components.add(component3)
1432+
bom.register_dependency(component1, [component3])
1433+
return bom
1434+
14041435
# ---
14051436

14061437

tests/_data/own/json/1.5/issue677.json

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_data/own/json/1.5/issue753.json

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.0" version="1">
3+
<components>
4+
<component type="library">
5+
<name>some-component</name>
6+
<version/>
7+
<modified>false</modified>
8+
</component>
9+
<component type="library">
10+
<name>some-library</name>
11+
<version/>
12+
<modified>false</modified>
13+
</component>
14+
<component type="library">
15+
<name>some-library</name>
16+
<version/>
17+
<modified>false</modified>
18+
</component>
19+
</components>
20+
</bom>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.1" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<components>
4+
<component type="library" bom-ref="some-component">
5+
<name>some-component</name>
6+
<version/>
7+
</component>
8+
<component type="library" bom-ref="some-library1">
9+
<name>some-library</name>
10+
<version/>
11+
</component>
12+
<component type="library" bom-ref="some-library2">
13+
<name>some-library</name>
14+
<version/>
15+
</component>
16+
</components>
17+
</bom>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"components": [
3+
{
4+
"bom-ref": "some-component",
5+
"name": "some-component",
6+
"type": "library",
7+
"version": ""
8+
},
9+
{
10+
"bom-ref": "some-library1",
11+
"name": "some-library",
12+
"type": "library",
13+
"version": ""
14+
},
15+
{
16+
"bom-ref": "some-library2",
17+
"name": "some-library",
18+
"type": "library",
19+
"version": ""
20+
}
21+
],
22+
"dependencies": [
23+
{
24+
"dependsOn": [
25+
"some-component"
26+
],
27+
"ref": "myApp"
28+
},
29+
{
30+
"dependsOn": [
31+
"some-library1",
32+
"some-library2"
33+
],
34+
"ref": "some-component"
35+
},
36+
{
37+
"ref": "some-library1"
38+
},
39+
{
40+
"ref": "some-library2"
41+
}
42+
],
43+
"metadata": {
44+
"component": {
45+
"bom-ref": "myApp",
46+
"name": "myApp",
47+
"type": "application",
48+
"version": ""
49+
},
50+
"timestamp": "2023-01-07T13:44:32.312678+00:00"
51+
},
52+
"serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac",
53+
"version": 1,
54+
"$schema": "http://cyclonedx.org/schema/bom-1.2b.schema.json",
55+
"bomFormat": "CycloneDX",
56+
"specVersion": "1.2"
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.2" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<metadata>
4+
<timestamp>2023-01-07T13:44:32.312678+00:00</timestamp>
5+
<component type="application" bom-ref="myApp">
6+
<name>myApp</name>
7+
<version/>
8+
</component>
9+
</metadata>
10+
<components>
11+
<component type="library" bom-ref="some-component">
12+
<name>some-component</name>
13+
<version/>
14+
</component>
15+
<component type="library" bom-ref="some-library1">
16+
<name>some-library</name>
17+
<version/>
18+
</component>
19+
<component type="library" bom-ref="some-library2">
20+
<name>some-library</name>
21+
<version/>
22+
</component>
23+
</components>
24+
<dependencies>
25+
<dependency ref="myApp">
26+
<dependency ref="some-component"/>
27+
</dependency>
28+
<dependency ref="some-component">
29+
<dependency ref="some-library1"/>
30+
<dependency ref="some-library2"/>
31+
</dependency>
32+
<dependency ref="some-library1"/>
33+
<dependency ref="some-library2"/>
34+
</dependencies>
35+
</bom>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"components": [
3+
{
4+
"bom-ref": "some-component",
5+
"name": "some-component",
6+
"type": "library",
7+
"version": ""
8+
},
9+
{
10+
"bom-ref": "some-library1",
11+
"name": "some-library",
12+
"type": "library",
13+
"version": ""
14+
},
15+
{
16+
"bom-ref": "some-library2",
17+
"name": "some-library",
18+
"type": "library",
19+
"version": ""
20+
}
21+
],
22+
"dependencies": [
23+
{
24+
"dependsOn": [
25+
"some-component"
26+
],
27+
"ref": "myApp"
28+
},
29+
{
30+
"dependsOn": [
31+
"some-library1",
32+
"some-library2"
33+
],
34+
"ref": "some-component"
35+
},
36+
{
37+
"ref": "some-library1"
38+
},
39+
{
40+
"ref": "some-library2"
41+
}
42+
],
43+
"metadata": {
44+
"component": {
45+
"bom-ref": "myApp",
46+
"name": "myApp",
47+
"type": "application",
48+
"version": ""
49+
},
50+
"timestamp": "2023-01-07T13:44:32.312678+00:00"
51+
},
52+
"serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac",
53+
"version": 1,
54+
"$schema": "http://cyclonedx.org/schema/bom-1.3a.schema.json",
55+
"bomFormat": "CycloneDX",
56+
"specVersion": "1.3"
57+
}

0 commit comments

Comments
 (0)