Skip to content

Commit 48ae201

Browse files
committed
work to get (de-)serialization working for Dependencies given CycloneDX/specification#146
Signed-off-by: Paul Horton <[email protected]>
1 parent 0f85fd4 commit 48ae201

18 files changed

+86
-102
lines changed

cyclonedx/model/dependency.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Copyright (c) OWASP Foundation. All Rights Reserved.
1919

2020
from abc import ABC, abstractmethod
21-
from typing import Any, Iterable, Optional, Set
21+
from typing import Any, Iterable, List, Optional, Set
2222

2323
import serializable
2424
from sortedcontainers import SortedSet
@@ -29,6 +29,24 @@
2929
from .bom_ref import BomRef
3030

3131

32+
class DependencyDependencies(serializable.BaseHelper): # type: ignore
33+
34+
@classmethod
35+
def serialize(cls, o: object) -> List[str]:
36+
if isinstance(o, SortedSet):
37+
return list(map(lambda i: str(i.ref), o))
38+
39+
raise ValueError(f'Attempt to serialize a non-Dependency: {o.__class__}')
40+
41+
@classmethod
42+
def deserialize(cls, o: object) -> Set["Dependency"]:
43+
dependencies: Set["Dependency"] = set()
44+
if isinstance(o, list):
45+
for v in o:
46+
dependencies.add(Dependency(ref=BomRef(value=v)))
47+
return dependencies
48+
49+
3250
@serializable.serializable_class
3351
class Dependency:
3452
"""
@@ -54,6 +72,7 @@ def ref(self, ref: BomRef) -> None:
5472

5573
@property # type: ignore[misc]
5674
@serializable.json_name('dependsOn')
75+
@serializable.type_mapping(DependencyDependencies)
5776
@serializable.xml_array(serializable.XmlArraySerializationType.FLAT, 'dependency')
5877
def dependencies(self) -> "SortedSet[Dependency]":
5978
return self._dependencies

poetry.lock

+31-46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ keywords = [
4848
python = "^3.7"
4949
importlib-metadata = { version = ">= 3.4", python = "< 3.8" }
5050
packageurl-python = ">= 0.9"
51-
py-serializable = "^0.9.2"
51+
py-serializable = "^0.10.0"
5252
setuptools = ">= 47.0.0"
5353
toml = "^0.10.0"
5454
sortedcontainers = "^2.4.0"

requirements.lowest.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# see pyptoject file for ranges
33

44
packageurl-python == 0.9.0
5-
py-serializable == 0.9.2
5+
py-serializable == 0.10.0
66
importlib-metadata == 3.4.0 # ; python_version < '3.8'
77
setuptools == 47.0.0
88
types-setuptools == 57.0.0

tests/data.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ def get_bom_with_component_setuptools_with_release_notes() -> Bom:
120120

121121
def get_bom_with_dependencies_valid() -> Bom:
122122
c1 = get_component_setuptools_simple()
123-
return Bom(components=[
124-
c1,
125-
get_component_toml_with_hashes_with_references()
126-
], dependencies=[
127-
Dependency(ref=c1.bom_ref, dependencies=[
128-
Dependency(ref=get_component_toml_with_hashes_with_references().bom_ref)
129-
]),
130-
Dependency(ref=get_component_toml_with_hashes_with_references().bom_ref)
131-
])
123+
c2 = get_component_toml_with_hashes_with_references()
124+
return Bom(
125+
components=[c1, c2], dependencies=[
126+
Dependency(ref=c1.bom_ref, dependencies=[
127+
Dependency(ref=c2.bom_ref)
128+
]),
129+
Dependency(ref=c2.bom_ref)
130+
]
131+
)
132132

133133

134134
def get_bom_with_dependencies_invalid() -> Bom:

tests/fixtures/json/1.2/bom_dependencies.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
]
5858
},
5959
{
60-
"ref": "pkg:pypi/[email protected]?extension=tar.gz",
61-
"dependsOn": []
60+
"ref": "pkg:pypi/[email protected]?extension=tar.gz"
6261
}
6362
]
6463
}

tests/fixtures/json/1.2/bom_dependencies_component.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
]
5858
},
5959
{
60-
"ref": "pkg:pypi/[email protected]?extension=tar.gz",
61-
"dependsOn": []
60+
"ref": "pkg:pypi/[email protected]?extension=tar.gz"
6261
}
6362
]
6463
}

tests/fixtures/json/1.2/bom_issue_275_components.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151
]
5252
},
5353
{
54-
"ref": "17e3b199-dc0b-42ef-bfdd-1fa81a1e3eda",
55-
"dependsOn": []
54+
"ref": "17e3b199-dc0b-42ef-bfdd-1fa81a1e3eda"
5655
},
5756
{
5857
"ref": "0b049d09-64c0-4490-a0f5-c84d9aacf857",
@@ -61,8 +60,7 @@
6160
]
6261
},
6362
{
64-
"ref": "cd3e9c95-9d41-49e7-9924-8cf0465ae789",
65-
"dependsOn": []
63+
"ref": "cd3e9c95-9d41-49e7-9924-8cf0465ae789"
6664
}
6765
]
6866
}

tests/fixtures/json/1.3/bom_dependencies.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@
6363
]
6464
},
6565
{
66-
"ref": "pkg:pypi/[email protected]?extension=tar.gz",
67-
"dependsOn": []
66+
"ref": "pkg:pypi/[email protected]?extension=tar.gz"
6867
}
6968
]
7069
}

tests/fixtures/json/1.3/bom_dependencies_component.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@
6363
]
6464
},
6565
{
66-
"ref": "pkg:pypi/[email protected]?extension=tar.gz",
67-
"dependsOn": []
66+
"ref": "pkg:pypi/[email protected]?extension=tar.gz"
6867
}
6968
]
7069
}

tests/fixtures/json/1.3/bom_issue_275_components.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151
]
5252
},
5353
{
54-
"ref": "17e3b199-dc0b-42ef-bfdd-1fa81a1e3eda",
55-
"dependsOn": []
54+
"ref": "17e3b199-dc0b-42ef-bfdd-1fa81a1e3eda"
5655
},
5756
{
5857
"ref": "0b049d09-64c0-4490-a0f5-c84d9aacf857",
@@ -61,8 +60,7 @@
6160
]
6261
},
6362
{
64-
"ref": "cd3e9c95-9d41-49e7-9924-8cf0465ae789",
65-
"dependsOn": []
63+
"ref": "cd3e9c95-9d41-49e7-9924-8cf0465ae789"
6664
}
6765
]
6866
}

tests/fixtures/json/1.4/bom_dependencies.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
33
"bomFormat": "CycloneDX",
44
"specVersion": "1.4",
5-
"serialNumber": "urn:uuid:93b888f9-e5a3-4e72-85ac-12dae1fb1ba8",
5+
"serialNumber": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
66
"version": 1,
77
"metadata": {
8-
"timestamp": "2023-01-07T13:45:58.745228+00:00",
8+
"timestamp": "2023-01-07T13:44:32.312678+00:00",
99
"tools": [
1010
{
1111
"vendor": "CycloneDX",

tests/fixtures/json/1.4/bom_dependencies_component.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@
9797
]
9898
},
9999
{
100-
"ref": "pkg:pypi/[email protected]?extension=tar.gz",
101-
"dependsOn": []
100+
"ref": "pkg:pypi/[email protected]?extension=tar.gz"
102101
}
103102
]
104103
}

0 commit comments

Comments
 (0)