Skip to content

Commit 623056e

Browse files
committed
fix: do not emit empty tools repo
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 4a2ac65 commit 623056e

File tree

38 files changed

+579
-10
lines changed

38 files changed

+579
-10
lines changed

cyclonedx/model/tool.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,14 @@ def __supports_components_and_services(view: Any) -> bool:
280280
def json_normalize(cls, o: ToolsRepository, *,
281281
view: Optional[Type['ViewType']],
282282
**__: Any) -> Any:
283-
if not o:
284-
return None
285283
if len(o.tools) > 0 or not cls.__supports_components_and_services(view):
286-
return cls.__all_as_tools(o)
287-
return {
288-
'components': tuple(o.components) if len(o.components) > 0 else None,
289-
'services': tuple(o.services) if len(o.services) > 0 else None,
290-
}
284+
return cls.__all_as_tools(o) or None
285+
elem = {}
286+
if o.components:
287+
elem['components'] = tuple(o.components)
288+
if o.services:
289+
elem['services'] = tuple(o.services)
290+
return elem or None
291291

292292
@classmethod
293293
def json_denormalize(cls, o: Union[List[Dict[str, Any]], Dict[str, Any]],
@@ -310,8 +310,6 @@ def xml_normalize(cls, o: ToolsRepository, *,
310310
view: Optional[Type['ViewType']],
311311
xmlns: Optional[str],
312312
**__: Any) -> Optional[Element]:
313-
if not o:
314-
return None
315313
elem = Element(element_name)
316314
if len(o.tools) > 0 or not cls.__supports_components_and_services(view):
317315
elem.extend(
@@ -334,7 +332,9 @@ def xml_normalize(cls, o: ToolsRepository, *,
334332
view_=view, as_string=False, element_name='service', xmlns=xmlns)
335333
for si in o.services)
336334
elem.append(elem_s)
337-
return elem
335+
return elem \
336+
if len(elem) > 0 \
337+
else None
338338

339339
@classmethod
340340
def xml_denormalize(cls, o: Element, *,

tests/_data/models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,31 @@ def get_bom_with_multiple_licenses() -> Bom:
10461046
licenses=multi_licenses)]
10471047
)
10481048

1049+
def get_bom_with_tools() -> Bom:
1050+
return _make_bom(
1051+
metadata=BomMetaData(
1052+
tools=[Tool(name='test-tool', version='1.33.7')]
1053+
)
1054+
)
1055+
1056+
def get_bom_with_tools_with_component_migrate() -> Bom:
1057+
return _make_bom(
1058+
metadata=BomMetaData(
1059+
tools=ToolsRepository(
1060+
components=[Component(type=ComponentType.APPLICATION, author='adobe',
1061+
name='test-component', version='1.2.3', bom_ref='my-component')]
1062+
)
1063+
)
1064+
)
1065+
1066+
def get_bom_with_tools_with_service_migrate() -> Bom:
1067+
return _make_bom(
1068+
metadata=BomMetaData(
1069+
tools=ToolsRepository(
1070+
services=[Service(name='test-service', bom_ref='my-service')]
1071+
)
1072+
)
1073+
)
10491074

10501075
def get_bom_with_tools_with_component_and_service_migrate() -> Bom:
10511076
return _make_bom(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.0" version="1">
3+
<components/>
4+
</bom>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.1" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<components/>
4+
</bom>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"metadata": {
3+
"timestamp": "2023-01-07T13:44:32.312678+00:00",
4+
"tools": [
5+
{
6+
"name": "test-tool",
7+
"version": "1.33.7"
8+
}
9+
]
10+
},
11+
"serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac",
12+
"version": 1,
13+
"$schema": "http://cyclonedx.org/schema/bom-1.2b.schema.json",
14+
"bomFormat": "CycloneDX",
15+
"specVersion": "1.2"
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.2" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<metadata>
4+
<timestamp>2023-01-07T13:44:32.312678+00:00</timestamp>
5+
<tools>
6+
<tool>
7+
<name>test-tool</name>
8+
<version>1.33.7</version>
9+
</tool>
10+
</tools>
11+
</metadata>
12+
</bom>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"metadata": {
3+
"timestamp": "2023-01-07T13:44:32.312678+00:00",
4+
"tools": [
5+
{
6+
"name": "test-tool",
7+
"version": "1.33.7"
8+
}
9+
]
10+
},
11+
"serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac",
12+
"version": 1,
13+
"$schema": "http://cyclonedx.org/schema/bom-1.3a.schema.json",
14+
"bomFormat": "CycloneDX",
15+
"specVersion": "1.3"
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.3" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<metadata>
4+
<timestamp>2023-01-07T13:44:32.312678+00:00</timestamp>
5+
<tools>
6+
<tool>
7+
<name>test-tool</name>
8+
<version>1.33.7</version>
9+
</tool>
10+
</tools>
11+
</metadata>
12+
</bom>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"metadata": {
3+
"timestamp": "2023-01-07T13:44:32.312678+00:00",
4+
"tools": [
5+
{
6+
"name": "test-tool",
7+
"version": "1.33.7"
8+
}
9+
]
10+
},
11+
"serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac",
12+
"version": 1,
13+
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
14+
"bomFormat": "CycloneDX",
15+
"specVersion": "1.4"
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
<bom xmlns="http://cyclonedx.org/schema/bom/1.4" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
3+
<metadata>
4+
<timestamp>2023-01-07T13:44:32.312678+00:00</timestamp>
5+
<tools>
6+
<tool>
7+
<name>test-tool</name>
8+
<version>1.33.7</version>
9+
</tool>
10+
</tools>
11+
</metadata>
12+
</bom>

0 commit comments

Comments
 (0)