Skip to content

Commit 04511f3

Browse files
committed
updates based on feedback from @jkowalleck
Signed-off-by: Paul Horton <[email protected]>
1 parent 8fb408c commit 04511f3

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

cyclonedx/model/bom.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
# SPDX-License-Identifier: Apache-2.0
1818
# Copyright (c) OWASP Foundation. All Rights Reserved.
19-
19+
import warnings
2020
from datetime import datetime, timezone
2121
from typing import Iterable, Optional, Set
2222
from uuid import UUID, uuid4
@@ -374,13 +374,25 @@ def validate(self) -> bool:
374374
"""
375375

376376
# 1. Make sure dependencies are all in this Bom.
377-
all_component_bom_refs = set(map(lambda c: c.bom_ref, self.components))
377+
all_bom_refs = set([self.metadata.component.bom_ref] if self.metadata.component else []).union(
378+
set(map(lambda c: c.bom_ref, self.components)),
379+
set(map(lambda s: s.bom_ref, self.services))
380+
)
378381
all_dependency_bom_refs = set().union(*(c.dependencies for c in self.components))
379-
dependency_diff = list(all_dependency_bom_refs.difference(all_component_bom_refs))
382+
dependency_diff = list(all_dependency_bom_refs.difference(all_bom_refs))
380383
if len(dependency_diff) > 0:
381384
raise UnknownComponentDependencyException(
382-
f'One or more Components have Dependency references to Components that are not known in this BOM. '
383-
f'They are: {dependency_diff}')
385+
f'One or more Components have Dependency references to Components/Services that are not known in this '
386+
f'BOM. They are: {dependency_diff}')
387+
388+
# 2. Dependencies should exist for the Component this BOM is describing, if one is set
389+
if self.metadata.component and not self.metadata.component.dependencies:
390+
warnings.warn(
391+
f'The Component this BOM is describing {self.metadata.component.purl} has no defined dependencies'
392+
f'which means the Dependency Graph is incomplete - you should add direct dependencies to this Component'
393+
f'to complete the Dependency Graph data.',
394+
UserWarning
395+
)
384396

385397
return True
386398

cyclonedx/output/json.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ def generate(self, force_regeneration: bool = False) -> None:
6666
extras = {}
6767
if self.bom_supports_dependencies():
6868
dependencies: List[Dict[str, Union[str, List[str]]]] = []
69+
if self.get_bom().metadata.component:
70+
dependencies.append({
71+
'ref': str(cast(Component, self.get_bom().metadata.component).bom_ref),
72+
'dependsOn': []
73+
})
6974
if self.get_bom().components:
7075
for component in self.get_bom().components:
7176
dependencies.append({
7277
'ref': str(component.bom_ref),
73-
'dependsOn': list(map(lambda x: str(x), component.dependencies))
78+
'dependsOn': [*map(str, component.dependencies)]
7479
})
7580
if dependencies:
7681
extras["dependencies"] = dependencies

cyclonedx/output/xml.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Copyright (c) OWASP Foundation. All Rights Reserved.
1919

2020
import warnings
21-
from typing import Optional, Set
21+
from typing import Optional, Set, cast
2222
from xml.etree import ElementTree
2323

2424
from ..model import (
@@ -111,6 +111,10 @@ def generate(self, force_regeneration: bool = False) -> None:
111111

112112
if self.bom_supports_dependencies() and self.get_bom().components:
113113
dependencies_element = ElementTree.SubElement(self._root_bom_element, 'dependencies')
114+
if self.get_bom().metadata.component:
115+
ElementTree.SubElement(dependencies_element, 'dependency', {
116+
'ref': str(cast(Component, self.get_bom().metadata.component).bom_ref)
117+
})
114118
for component in self.get_bom().components:
115119
dependency_element = ElementTree.SubElement(dependencies_element, 'dependency', {
116120
'ref': str(component.bom_ref)

0 commit comments

Comments
 (0)