Skip to content

Commit be95494

Browse files
fix dependency warning for root component only #617
Signed-off-by: weichslgartner <[email protected]>
1 parent 3431d46 commit be95494

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

cyclonedx/model/bom.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,9 @@ def validate(self) -> bool:
667667
'One or more Components have Dependency references to Components/Services that are not known in this '
668668
f'BOM. They are: {dependency_diff}')
669669

670-
# 2. if root component is set: dependencies should exist for the Component this BOM is describing
671-
if self.metadata.component and not any(map(
670+
# 2. if root component is set and there are other components: dependencies should exist for the Component
671+
# this BOM is describing
672+
if self.metadata.component and len(self.components) > 0 and not any(map(
672673
lambda d: d.ref == self.metadata.component.bom_ref and len(d.dependencies) > 0, # type: ignore[union-attr]
673674
self.dependencies
674675
)):

tests/test_model_bom.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
17-
18-
17+
import warnings
1918
from typing import Callable, Tuple
2019
from unittest import TestCase
2120
from uuid import uuid4
@@ -29,6 +28,7 @@
2928
from cyclonedx.model.component import Component, ComponentType
3029
from cyclonedx.model.contact import OrganizationalContact, OrganizationalEntity
3130
from cyclonedx.model.license import DisjunctiveLicense
31+
from cyclonedx.output.json import JsonV1Dot6
3232
from tests._data.models import (
3333
get_bom_component_licenses_invalid,
3434
get_bom_component_nested_licenses_invalid,
@@ -133,6 +133,15 @@ def test_empty_bom(self) -> None:
133133
self.assertFalse(bom.services)
134134
self.assertFalse(bom.external_references)
135135

136+
def test_root_component_only_bom(self) -> None:
137+
with warnings.catch_warnings():
138+
warnings.simplefilter('error', UserWarning) # Turn UserWarnings into errors
139+
try:
140+
bom = Bom(metadata=BomMetaData(component=Component(name='test', version='1.2')))
141+
_ = JsonV1Dot6(bom).output_as_string()
142+
except UserWarning as e:
143+
self.fail(f"A warning with 'warn' was issued: {e}")
144+
136145
def test_empty_bom_defined_serial(self) -> None:
137146
serial_number = uuid4()
138147
bom = Bom(serial_number=serial_number)

0 commit comments

Comments
 (0)