Skip to content

Commit 3c2f2c2

Browse files
Merge branch 'main' into feat/model-card
2 parents 3616ef0 + bdeaa91 commit 3c2f2c2

29 files changed

Lines changed: 1298 additions & 45 deletions

CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,41 @@
22

33
<!-- version list -->
44

5+
## v11.10.0 (2026-06-11)
6+
7+
### Bug Fixes
8+
9+
- Lossless flattening of dependency graph during JSON serialization
10+
([#993](https://github.com/CycloneDX/cyclonedx-python-lib/pull/993),
11+
[`d0e10ca`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/d0e10ca147dfece8bd7c76b104d7579255879679))
12+
13+
- Typing in `contrib.bom.utils.BomDependencyGraphFlatMerger`
14+
([#998](https://github.com/CycloneDX/cyclonedx-python-lib/pull/998),
15+
[`988a937`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/988a9372a676358b29e67573c9f39d94121824bc))
16+
17+
### Documentation
18+
19+
- Improve docs of `contrib.bom.utils.BomRefDiscriminator`
20+
([#996](https://github.com/CycloneDX/cyclonedx-python-lib/pull/996),
21+
[`9beaf5c`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/9beaf5c6b35a8739727572c0e351063849d876fc))
22+
23+
### Features
24+
25+
- Add `contrib.bom.utils.BomDependencyGraphFlatMerger`
26+
([#997](https://github.com/CycloneDX/cyclonedx-python-lib/pull/997),
27+
[`78b8d8b`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/78b8d8b94b26df14d0194b3d44a5db8a76f1fa47))
28+
29+
- Move `output.BomRefDiscriminator` to `contrib.bom.utils.BomRefDiscriminator`
30+
([#995](https://github.com/CycloneDX/cyclonedx-python-lib/pull/995),
31+
[`3bb87aa`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/3bb87aabbc421aabac307305217c7975e63b43eb))
32+
33+
### Performance Improvements
34+
35+
- `contrib.bom.utils.bomdependencygraphflatmerger._flatten_merge`
36+
([#999](https://github.com/CycloneDX/cyclonedx-python-lib/pull/999),
37+
[`a8579b8`](https://github.com/CycloneDX/cyclonedx-python-lib/commit/a8579b87f7e121ebbec16b9f05bdb3b4de11e71c))
38+
39+
540
## v11.9.0 (2026-06-08)
641

742
### Features

cyclonedx/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222

2323
# !! version is managed by semantic_release
2424
# do not use typing here, or else `semantic_release` might have issues finding the variable
25-
__version__ = "11.9.0" # noqa:Q000
25+
__version__ = "11.10.0" # noqa:Q000

cyclonedx/contrib/bom/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is part of CycloneDX Python Library
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
18+
"""Bom related functionality"""

cyclonedx/contrib/bom/utils.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# This file is part of CycloneDX Python Library
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
18+
"""Bom related utilities"""
19+
20+
__all__ = ['BomRefDiscriminator', 'BomDependencyGraphFlatMerger']
21+
22+
from collections.abc import Iterable
23+
from itertools import chain
24+
from random import random
25+
from typing import TYPE_CHECKING, Any
26+
27+
from ...model.dependency import Dependency
28+
29+
if TYPE_CHECKING: # pragma: no cover
30+
from ...model.bom import Bom
31+
from ...model.bom_ref import BomRef
32+
33+
34+
class BomRefDiscriminator:
35+
"""
36+
Ensure that a collection of BomRef objects
37+
has unique, non‑empty :attr:`cyclonedx.model.bom_ref.BomRef.value`.
38+
39+
The discriminator inspects each provided BomRef and assigns a newly
40+
generated identifier to any instance whose ``value`` is missing or
41+
duplicates an earlier one.
42+
All original values are preserved and can be restored via :meth:`reset()`
43+
or by using this class as a context manager.
44+
"""
45+
46+
def __init__(self, bomrefs: Iterable['BomRef'], prefix: str = 'BomRef') -> None:
47+
# NOTE: do not use dict/set here, different BomRefs with same value
48+
# have same hash and would shadow each other.
49+
self._bomrefs = tuple((bomref, bomref.value) for bomref in bomrefs)
50+
self._prefix = prefix
51+
52+
def __enter__(self) -> None:
53+
self.discriminate()
54+
55+
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
56+
self.reset()
57+
58+
def discriminate(self) -> None:
59+
"""
60+
Enforce uniqueness across all
61+
:attr:`cyclonedx.model.bom_ref.BomRef.value`s.
62+
63+
Any BomRef whose ``value`` is ``None`` or duplicates a previously
64+
encountered value is assigned a newly generated unique identifier.
65+
"""
66+
known_values = []
67+
for bomref, _ in self._bomrefs:
68+
value = bomref.value
69+
if value is None or value in known_values:
70+
value = self._make_unique()
71+
bomref.value = value
72+
known_values.append(value)
73+
74+
def reset(self) -> None:
75+
"""
76+
Restore all :attr:`cyclonedx.model.bom_ref.BomRef.value`s to
77+
their original state.
78+
"""
79+
for bomref, original_value in self._bomrefs:
80+
bomref.value = original_value
81+
82+
def _make_unique(self) -> str:
83+
return f'{self._prefix}{str(random())[1:]}{str(random())[1:]}' # nosec B311
84+
85+
@classmethod
86+
def from_bom(cls, bom: 'Bom', prefix: str = 'BomRef') -> 'BomRefDiscriminator':
87+
"""
88+
Create a discriminator for all :class:`cyclonedx.model.bom_ref.BomRefs`
89+
contained within a Bom.
90+
91+
This includes BomRefs from
92+
* :attr:`cyclonedx.model.bom.Bom.components`
93+
* :attr:`cyclonedx.model.bom.Bom.services`
94+
* :attr:`cyclonedx.model.bom.Bom.vulnerabilities`
95+
"""
96+
return cls(chain(
97+
map(lambda c: c.bom_ref, bom._get_all_components()),
98+
map(lambda s: s.bom_ref, bom.services),
99+
map(lambda v: v.bom_ref, bom.vulnerabilities)
100+
), prefix)
101+
102+
103+
class BomDependencyGraphFlatMerger:
104+
"""
105+
Context‑manager utility that temporarily flattens and merges all
106+
:attr:`cyclonedx.model.bom.Bom.dependencies`.
107+
108+
When used as a context manager, the :class:`cyclonedx.model.bom.Bom`'s
109+
dependency graph is replaced with a flattened, merged representation
110+
for the duration of the ``with`` block and automatically restored
111+
afterward.
112+
"""
113+
114+
def __init__(self, bom: 'Bom') -> None:
115+
self._bom = bom
116+
# NOTE: do not use the getter - see `reset()` for reasons.
117+
self._deps = self._bom._dependencies
118+
119+
def __enter__(self) -> None:
120+
self.flatten_merge()
121+
122+
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
123+
self.reset()
124+
125+
def flatten_merge(self) -> None:
126+
"""
127+
Flatten and merge all :attr:`cyclonedx.model.bom.Bom.dependencies`.
128+
129+
This produces a non‑recursive, merged representation of the entire
130+
dependency graph and assigns it to the Bom.
131+
132+
.. note::
133+
The original dependency graph is not modified. A new, flattened
134+
dependency structure is assigned to the Bom.
135+
"""
136+
self._bom.dependencies = self._flatten_merge(self._deps)
137+
138+
def reset(self) -> None:
139+
"""
140+
Restore the :class:`cyclonedx.model.bom.Bom`'s dependency graph to
141+
its original state.
142+
143+
.. note::
144+
This does not modify the dependency graph. It simply reassigns
145+
the original dependency collection back to the Bom.
146+
"""
147+
# NOTE: not using the setter, which would create overhead,
148+
# and - most importantly - this could cause deduplication of an existing malformed set.
149+
# Just access the internal field directly!
150+
self._bom._dependencies = self._deps
151+
152+
@staticmethod
153+
def _flatten_merge(deps: Iterable[Dependency]) -> Iterable[Dependency]:
154+
flat: dict['BomRef', list['BomRef']] = {}
155+
todos = list(deps)
156+
seen: set[int] = set()
157+
while todos:
158+
todo = todos.pop()
159+
if (todo_id := id(todo)) in seen:
160+
continue
161+
seen.add(todo_id)
162+
ds = flat.setdefault(todo.ref, [])
163+
if todo_deps := todo.dependencies:
164+
ds.extend(d.ref for d in todo_deps)
165+
todos.extend(todo_deps)
166+
return (
167+
Dependency(br, (Dependency(d) for d in ds))
168+
for br, ds
169+
in flat.items()
170+
)

cyclonedx/output/__init__.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222
"""
2323

2424
import os
25+
import sys
2526
from abc import ABC, abstractmethod
26-
from collections.abc import Iterable, Mapping
27-
from itertools import chain
28-
from random import random
27+
from collections.abc import Mapping
2928
from typing import TYPE_CHECKING, Any, Literal, Optional, Union, overload
3029

30+
if sys.version_info >= (3, 13):
31+
from warnings import deprecated
32+
else:
33+
from typing_extensions import deprecated
34+
35+
from ..contrib.bom.utils import BomRefDiscriminator as _BomRefDiscriminator
3136
from ..schema import OutputFormat, SchemaVersion
3237

3338
if TYPE_CHECKING: # pragma: no cover
3439
from ..model.bom import Bom
35-
from ..model.bom_ref import BomRef
3640
from .json import Json as JsonOutputter
3741
from .xml import Xml as XmlOutputter
3842

@@ -138,40 +142,19 @@ def make_outputter(bom: 'Bom', output_format: OutputFormat, schema_version: Sche
138142
raise ValueError(f'Unknown {output_format.name}/schema_version: {schema_version!r}')
139143
return klass(bom)
140144

145+
# region deprecated re-export
141146

142-
class BomRefDiscriminator:
143-
144-
def __init__(self, bomrefs: Iterable['BomRef'], prefix: str = 'BomRef') -> None:
145-
# do not use dict/set here, different BomRefs with same value have same hash and would shadow each other
146-
self._bomrefs = tuple((bomref, bomref.value) for bomref in bomrefs)
147-
self._prefix = prefix
148-
149-
def __enter__(self) -> None:
150-
self.discriminate()
151147

152-
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
153-
self.reset()
148+
@deprecated('Deprecated re-export location - see docstring of "BomRefDiscriminator" for details.')
149+
class BomRefDiscriminator(_BomRefDiscriminator):
150+
"""Deprecated — Alias of :class:`cyclonedx.contrib.bom.utils.BomRefDiscriminator`.
154151
155-
def discriminate(self) -> None:
156-
known_values = []
157-
for bomref, _ in self._bomrefs:
158-
value = bomref.value
159-
if value is None or value in known_values:
160-
value = self._make_unique()
161-
bomref.value = value
162-
known_values.append(value)
163-
164-
def reset(self) -> None:
165-
for bomref, original_value in self._bomrefs:
166-
bomref.value = original_value
152+
.. deprecated:: next
153+
This re-export location is deprecated.
154+
Use ``from cyclonedx.contrib.bom.utils import BomRefDiscriminator`` instead.
155+
The exported symbol itself is NOT deprecated — only this import path.
156+
"""
157+
pass
167158

168-
def _make_unique(self) -> str:
169-
return f'{self._prefix}{str(random())[1:]}{str(random())[1:]}' # nosec B311
170159

171-
@classmethod
172-
def from_bom(cls, bom: 'Bom', prefix: str = 'BomRef') -> 'BomRefDiscriminator':
173-
return cls(chain(
174-
map(lambda c: c.bom_ref, bom._get_all_components()),
175-
map(lambda s: s.bom_ref, bom.services),
176-
map(lambda v: v.bom_ref, bom.vulnerabilities)
177-
), prefix)
160+
# endregion deprecated re-export

cyclonedx/output/json.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from json import dumps as json_dumps, loads as json_loads
2020
from typing import TYPE_CHECKING, Any, Literal, Optional, Union
2121

22+
from ..contrib.bom.utils import BomDependencyGraphFlatMerger, BomRefDiscriminator
2223
from ..exception.output import FormatNotSupportedException
2324
from ..schema import OutputFormat, SchemaVersion
2425
from ..schema.schema import (
@@ -33,7 +34,7 @@
3334
SchemaVersion1Dot6,
3435
SchemaVersion1Dot7,
3536
)
36-
from . import BaseOutput, BomRefDiscriminator
37+
from . import BaseOutput
3738

3839
if TYPE_CHECKING: # pragma: no cover
3940
from ..model.bom import Bom
@@ -71,9 +72,10 @@ def generate(self, force_regeneration: bool = False) -> None:
7172
bom = self.get_bom()
7273
bom.validate()
7374
with BomRefDiscriminator.from_bom(bom):
74-
bom_json: dict[str, Any] = json_loads(
75-
bom.as_json( # type:ignore[attr-defined]
76-
view_=_view))
75+
with BomDependencyGraphFlatMerger(bom):
76+
bom_json: dict[str, Any] = json_loads(
77+
bom.as_json( # type:ignore[attr-defined]
78+
view_=_view))
7779
bom_json.update(_json_core)
7880
self._bom_json = bom_json
7981
self.generated = True

cyclonedx/output/xml.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from xml.dom.minidom import parseString as dom_parseString # nosec B408
2121
from xml.etree.ElementTree import Element as XmlElement, tostring as xml_dumps # nosec B405
2222

23+
from ..contrib.bom.utils import BomRefDiscriminator
2324
from ..schema import OutputFormat, SchemaVersion
2425
from ..schema.schema import (
2526
SCHEMA_VERSIONS,
@@ -33,7 +34,7 @@
3334
SchemaVersion1Dot6,
3435
SchemaVersion1Dot7,
3536
)
36-
from . import BaseOutput, BomRefDiscriminator
37+
from . import BaseOutput
3738

3839
if TYPE_CHECKING: # pragma: no cover
3940
from ..model.bom import Bom

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
# The full version, including alpha/beta/rc tags
2525
# !! version is managed by semantic_release
26-
release = '11.9.0'
26+
release = '11.10.0'
2727

2828
# -- General configuration ---------------------------------------------------
2929

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
55
[tool.poetry]
66
name = "cyclonedx-python-lib"
77
# !! version is managed by semantic_release
8-
version = "11.9.0"
8+
version = "11.10.0"
99
description = "Python library for CycloneDX"
1010
authors = [
1111
"Paul Horton <phorton@sonatype.com>",

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from sortedcontainers import SortedSet
2727

28-
from cyclonedx.output import BomRefDiscriminator as _BomRefDiscriminator
28+
from cyclonedx.contrib.bom.utils import BomRefDiscriminator as _BomRefDiscriminator
2929
from cyclonedx.schema import OutputFormat, SchemaVersion
3030

3131
if TYPE_CHECKING:

0 commit comments

Comments
 (0)