Skip to content

Commit e762fc6

Browse files
authored
Merge pull request #6 from Roche-CSI/ToolInformationFixes
ToolInformation - Equals, HashCode, and multi-tool parsing fixes w/tests
2 parents 6bdf644 + a911424 commit e762fc6

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package org.cyclonedx.model.metadata;
22

33
import java.util.List;
4+
import java.util.Objects;
45

5-
import com.fasterxml.jackson.annotation.JsonInclude;
66
import org.cyclonedx.model.Component;
77
import org.cyclonedx.model.Service;
88

9+
import com.fasterxml.jackson.annotation.JsonInclude;
10+
911
@JsonInclude(JsonInclude.Include.NON_EMPTY)
1012
public class ToolInformation
1113
{
1214
private List<Component> components;
1315

1416
private List<Service> services;
1517

18+
1619
public List<Component> getComponents() {
1720
return components;
1821
}
@@ -21,11 +24,26 @@ public void setComponents(final List<Component> components) {
2124
this.components = components;
2225
}
2326

27+
2428
public List<Service> getServices() {
2529
return services;
2630
}
2731

2832
public void setServices(final List<Service> services) {
2933
this.services = services;
3034
}
35+
36+
@Override
37+
public int hashCode() {
38+
return Objects.hash(components, services);
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if (this == o) return true;
44+
if (o == null || getClass() != o.getClass()) return false;
45+
ToolInformation other = (ToolInformation) o;
46+
return Objects.equals(components, other.components) &&
47+
Objects.equals(services, other.services);
48+
}
3149
}

src/main/java/org/cyclonedx/util/deserializer/MetadataDeserializer.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ private void parseComponents(JsonNode componentsNode, ToolInformation toolInform
140140
List<Component> components = mapper.convertValue(componentsNode, new TypeReference<List<Component>>() {});
141141
toolInformation.setComponents(components);
142142
} else if (componentsNode.isObject()) {
143+
if (componentsNode.has("component")) {
144+
JsonNode componentNode = componentsNode.get("component");
145+
if (componentNode.isArray()) {
146+
parseComponents(componentNode, toolInformation);
147+
return;
148+
}
149+
}
143150
Component component = mapper.convertValue(componentsNode, Component.class);
144151
toolInformation.setComponents(Collections.singletonList(component));
145152
}
@@ -152,6 +159,13 @@ private void parseServices(JsonNode servicesNode, ToolInformation toolInformatio
152159
List<Service> services = mapper.convertValue(servicesNode, new TypeReference<List<Service>>() {});
153160
toolInformation.setServices(services);
154161
} else if (servicesNode.isObject()) {
162+
if (servicesNode.has("service")) {
163+
JsonNode serviceNode = servicesNode.get("service");
164+
if (serviceNode.isArray()) {
165+
parseServices(serviceNode, toolInformation);
166+
return;
167+
}
168+
}
155169
Service service = mapper.convertValue(servicesNode, Service.class);
156170
toolInformation.setServices(Collections.singletonList(service));
157171
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"bomFormat": "CycloneDX",
3+
"specVersion": "1.5",
4+
"serialNumber": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
5+
"version": 1,
6+
"metadata": {
7+
"tools": {
8+
"components": [
9+
{
10+
"type": "application",
11+
"group": "Awesome Vendor",
12+
"name": "Awesome Tool",
13+
"version": "9.1.2",
14+
"hashes": [
15+
{
16+
"alg": "SHA-1",
17+
"content": "25ed8e31b995bb927966616df2a42b979a2717f0"
18+
},
19+
{
20+
"alg": "SHA-256",
21+
"content": "a74f733635a19aefb1f73e5947cef59cd7440c6952ef0f03d09d974274cbd6df"
22+
}
23+
]
24+
},
25+
{
26+
"type": "application",
27+
"group": "Awesome Vendor2",
28+
"name": "Awesome Tool2",
29+
"version": "7.4.0"
30+
}
31+
],
32+
"services": [
33+
{
34+
"provider": {
35+
"name": "Acme Org",
36+
"url": [
37+
"https://example.com"
38+
]
39+
},
40+
"group": "com.example",
41+
"name": "Acme Signing Server",
42+
"description": "Signs artifacts",
43+
"endpoints": [
44+
"https://example.com/sign",
45+
"https://example.com/verify",
46+
"https://example.com/tsa"
47+
]
48+
},
49+
{
50+
"provider": {
51+
"name": "Example Org",
52+
"url": [
53+
"https://example.com"
54+
]
55+
},
56+
"group": "com.example",
57+
"name": "Some service",
58+
"description": "Provides services",
59+
"endpoints": [
60+
"https://example.com/whatever"
61+
]
62+
}
63+
]
64+
}
65+
},
66+
"components": []
67+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0"?>
2+
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.5">
3+
<metadata>
4+
<tools>
5+
<components>
6+
<component type="application">
7+
<group>Awesome Vendor</group>
8+
<name>Awesome Tool</name>
9+
<version>9.1.2</version>
10+
<hashes>
11+
<hash alg="SHA-1">25ed8e31b995bb927966616df2a42b979a2717f0</hash>
12+
<hash alg="SHA-256">a74f733635a19aefb1f73e5947cef59cd7440c6952ef0f03d09d974274cbd6df</hash>
13+
</hashes>
14+
</component>
15+
<component type="application">
16+
<group>Awesome Vendor2</group>
17+
<name>Awesome Tool2</name>
18+
<version>7.4.0</version>
19+
</component>
20+
</components>
21+
<services>
22+
<service>
23+
<provider>
24+
<name>Acme Org</name>
25+
<url>https://example.com</url>
26+
</provider>
27+
<group>com.example</group>
28+
<name>Acme Signing Server</name>
29+
<description>Signs artifacts</description>
30+
<endpoints>
31+
<endpoint>https://example.com/sign</endpoint>
32+
<endpoint>https://example.com/verify</endpoint>
33+
<endpoint>https://example.com/tsa</endpoint>
34+
</endpoints>
35+
</service>
36+
<service>
37+
<provider>
38+
<name>Example Org</name>
39+
<url>https://example.com</url>
40+
</provider>
41+
<group>com.example</group>
42+
<name>Some service</name>
43+
<description>Provides services</description>
44+
<endpoints>
45+
<endpoint>https://example.com/whatever</endpoint>
46+
</endpoints>
47+
</service>
48+
</services>
49+
</tools>
50+
</metadata>
51+
<components />
52+
</bom>

0 commit comments

Comments
 (0)