Skip to content

Commit 418571e

Browse files
committed
Use spec version to get the schema version class #1105
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent e591497 commit 418571e

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

scanpipe/pipes/cyclonedx.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
from cyclonedx.model import license as cdx_license_model
3131
from cyclonedx.schema import SchemaVersion
32+
from cyclonedx.schema.schema import SCHEMA_VERSIONS
3233
from cyclonedx.validation import ValidationError
3334
from cyclonedx.validation.json import JsonStrictValidator
3435
from packageurl import PackageURL
@@ -41,39 +42,36 @@ def get_bom(cyclonedx_document):
4142
return Bom.from_json(data=cyclonedx_document)
4243

4344

44-
def get_components(bom):
45+
def get_components(bom, view):
4546
"""Return list of components from CycloneDX BOM."""
4647
# TODO: Check if we could use `return list(bom._get_all_components())`
47-
return recursive_component_collector(bom.components, [])
48+
return recursive_component_collector(bom.components, [], view)
4849

4950

50-
def bom_attributes_to_dict(cyclonedx_attributes):
51+
def bom_attributes_to_dict(cyclonedx_attributes, view):
5152
"""Return list of dict from a list of CycloneDX attributes."""
52-
from cyclonedx.schema.schema import SchemaVersion1Dot5
53-
5453
if not cyclonedx_attributes:
5554
return []
5655

5756
return [
58-
json.loads(attribute.as_json(view_=SchemaVersion1Dot5)) # TODO
59-
for attribute in cyclonedx_attributes
57+
json.loads(attribute.as_json(view_=view)) for attribute in cyclonedx_attributes
6058
]
6159

6260

63-
def recursive_component_collector(root_component_list, collected):
61+
def recursive_component_collector(root_component_list, collected, view):
6462
"""Return list of components including the nested components."""
6563
if not root_component_list:
6664
return []
6765

6866
for component in root_component_list:
6967
nested_components = {}
7068
if component.components is not None:
71-
nested_components = bom_attributes_to_dict(component.components)
69+
nested_components = bom_attributes_to_dict(component.components, view)
7270

7371
collected.append(
7472
{"cdx_package": component, "nested_components": nested_components}
7573
)
76-
recursive_component_collector(component.components, collected)
74+
recursive_component_collector(component.components, collected, view)
7775
return collected
7876

7977

@@ -218,6 +216,11 @@ def resolve_cyclonedx_packages(input_location):
218216
)
219217

220218
cyclonedx_bom = get_bom(cyclonedx_document)
221-
components = get_components(cyclonedx_bom)
219+
220+
# Could we get this similar to Bom.from_json(data=cyclonedx_document)?
221+
spec_version = cyclonedx_document.get("specVersion")
222+
schema_version = SchemaVersion.from_version(spec_version)
223+
view = SCHEMA_VERSIONS.get(schema_version)
224+
components = get_components(cyclonedx_bom, view)
222225

223226
return [cyclonedx_component_to_package_data(component) for component in components]

0 commit comments

Comments
 (0)