Skip to content

Commit 1e0560f

Browse files
committed
Add Extra Validations
1 parent a2d30ea commit 1e0560f

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/main/java/org/cyclonedx/util/serializer/MetadataSerializer.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.cyclonedx.Version;
1212
import org.cyclonedx.model.Metadata;
1313
import org.cyclonedx.model.Property;
14-
import org.cyclonedx.model.component.evidence.Occurrence;
1514
import org.cyclonedx.model.metadata.ToolInformation;
1615

1716
import static org.cyclonedx.util.serializer.SerializerUtils.shouldSerializeField;
@@ -67,7 +66,7 @@ private void createMetadataInfo(
6766
//Tools
6867
parseTools(metadata, jsonGenerator);
6968

70-
if (metadata.getAuthors() != null && shouldSerializeField(metadata, version, "author")) {
69+
if (CollectionUtils.isNotEmpty(metadata.getAuthors()) && shouldSerializeField(metadata, version, "author")) {
7170
if (isXml) {
7271
ToXmlGenerator xmlGenerator = (ToXmlGenerator) jsonGenerator;
7372
writeArrayFieldXML(metadata.getAuthors(), xmlGenerator, "author");
@@ -132,18 +131,18 @@ else if (version.getVersion() >= Version.VERSION_15.getVersion()) {
132131
jsonGenerator.writeFieldName("tools");
133132
jsonGenerator.writeStartObject();
134133
if (isXml && jsonGenerator instanceof ToXmlGenerator) {
135-
if (choice.getComponents() != null) {
134+
if (CollectionUtils.isNotEmpty(choice.getComponents())) {
136135
writeArrayFieldXML(choice.getComponents(), (ToXmlGenerator) jsonGenerator, "component");
137136
}
138-
if (choice.getServices() != null) {
137+
if (CollectionUtils.isNotEmpty(choice.getServices())) {
139138
writeArrayFieldXML(choice.getServices(), (ToXmlGenerator) jsonGenerator, "service");
140139
}
141140
}
142141
else {
143-
if (choice.getComponents() != null) {
142+
if (CollectionUtils.isNotEmpty(choice.getComponents())) {
144143
writeArrayFieldJSON(jsonGenerator, "components", choice.getComponents());
145144
}
146-
if (choice.getServices() != null) {
145+
if (CollectionUtils.isNotEmpty(choice.getServices())) {
147146
writeArrayFieldJSON(jsonGenerator, "services", choice.getServices());
148147
}
149148
}

src/test/java/org/cyclonedx/BomJsonGeneratorTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,18 @@ public void testIssue408Regression_xmlToJson_externalReferenceBom() throws Excep
547547
assertTrue(parser.isValid(loadedFile, version));
548548
}
549549

550+
@Test
551+
public void testServicesMetadata() throws Exception {
552+
Version version = Version.VERSION_16;
553+
Bom bom = createCommonXmlBom("/regression/valid-metadata-tool-no-services.xml");
554+
555+
BomJsonGenerator generator = BomGeneratorFactory.createJson(version, bom);
556+
File loadedFile = writeToFile(generator.toJsonString());
557+
558+
JsonParser parser = new JsonParser();
559+
assertTrue(parser.isValid(loadedFile, version));
560+
}
561+
550562
@Test
551563
public void testIssue562() throws Exception {
552564
Version version = Version.VERSION_16;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.6">
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+
</components>
16+
</tools>
17+
</metadata>
18+
<components />
19+
</bom>

0 commit comments

Comments
 (0)