Skip to content

Commit 9aff926

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

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
@@ -675,8 +675,9 @@ def validate(self) -> bool:
675675
'One or more Components have Dependency references to Components/Services that are not known in this '
676676
f'BOM. They are: {dependency_diff}')
677677

678-
# 2. if root component is set: dependencies should exist for the Component this BOM is describing
679-
if self.metadata.component and not any(map(
678+
# 2. if root component is set and there are other components: dependencies should exist for the Component
679+
# this BOM is describing
680+
if self.metadata.component and len(self.components) > 0 and not any(map(
680681
lambda d: d.ref == self.metadata.component.bom_ref and len(d.dependencies) > 0, # type: ignore[union-attr]
681682
self.dependencies
682683
)):

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
@@ -31,6 +30,7 @@
3130
from cyclonedx.model.license import DisjunctiveLicense
3231
from cyclonedx.model.lifecycle import LifecyclePhase, NamedLifecycle, PredefinedLifecycle
3332
from cyclonedx.model.tool import Tool
33+
from cyclonedx.output.json import JsonV1Dot6
3434
from tests._data.models import (
3535
get_bom_component_licenses_invalid,
3636
get_bom_component_nested_licenses_invalid,
@@ -139,6 +139,15 @@ def test_empty_bom(self) -> None:
139139
self.assertFalse(bom.services)
140140
self.assertFalse(bom.external_references)
141141

142+
def test_root_component_only_bom(self) -> None:
143+
with warnings.catch_warnings():
144+
warnings.simplefilter('error', UserWarning) # Turn UserWarnings into errors
145+
try:
146+
bom = Bom(metadata=BomMetaData(component=Component(name='test', version='1.2')))
147+
_ = JsonV1Dot6(bom).output_as_string()
148+
except UserWarning as e:
149+
self.fail(f"A warning with 'warn' was issued: {e}")
150+
142151
def test_empty_bom_defined_serial(self) -> None:
143152
serial_number = uuid4()
144153
bom = Bom(serial_number=serial_number)

0 commit comments

Comments
 (0)