diff --git a/src/main/java/org/cyclonedx/util/serializer/LicenseChoiceSerializer.java b/src/main/java/org/cyclonedx/util/serializer/LicenseChoiceSerializer.java index ef6a27658..0d1f5fe4f 100644 --- a/src/main/java/org/cyclonedx/util/serializer/LicenseChoiceSerializer.java +++ b/src/main/java/org/cyclonedx/util/serializer/LicenseChoiceSerializer.java @@ -120,6 +120,9 @@ else if (StringUtils.isNotBlank(l.getName())) { } else if (lc.getExpression() != null) { serializeExpressionToXml(lc, toXmlGenerator); + } else { + toXmlGenerator.writeStartArray(); + toXmlGenerator.writeEndArray(); } } @@ -155,7 +158,10 @@ private void serializeJson( else if (licenseChoice.getExpression() != null && StringUtils.isNotBlank(licenseChoice.getExpression().getValue())) { serializeExpressionToJson(licenseChoice, gen); - } + } else { + gen.writeStartArray(); + gen.writeEndArray(); + } } private void serializeExpressionToXml( diff --git a/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java b/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java index 67c53b01e..10fac4626 100644 --- a/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java +++ b/src/test/java/org/cyclonedx/BomJsonGeneratorTest.java @@ -19,6 +19,7 @@ package org.cyclonedx; import com.fasterxml.jackson.databind.JsonNode; +import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import org.cyclonedx.generators.BomGeneratorFactory; import org.cyclonedx.generators.json.BomJsonGenerator; @@ -248,6 +249,29 @@ public void testIssue408Regression_xmlToJson() throws Exception { assertTrue(parser.isValid(loadedFile, version)); } + @Test + public void testIssue439Regression_jsonEmptyLicense() throws Exception { + Version version = Version.VERSION_16; + Bom bom = new Bom(); + bom.addComponent(getComponentWithEmptyLicenseChoice()); + + BomJsonGenerator generator = BomGeneratorFactory.createJson(version, bom); + String jsonString = generator.toJsonString(); + + assertFalse(jsonString.isEmpty()); + JsonParser parser = new JsonParser(); + assertTrue(parser.isValid(jsonString.getBytes(StandardCharsets.UTF_8))); + } + + private static Component getComponentWithEmptyLicenseChoice() { + Component component = new Component(); + component.setName("xalan"); + component.setType(Component.Type.LIBRARY); + component.setLicenses(new LicenseChoice()); + component.setPurl("pkg:maven/xalan/xalan@2.6.0?type=jar"); + return component; + } + @Test public void schema16_testEvidence() throws Exception { Version version = Version.VERSION_16; diff --git a/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java b/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java index f2fabeb7a..759486eff 100644 --- a/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java +++ b/src/test/java/org/cyclonedx/BomXmlGeneratorTest.java @@ -18,6 +18,7 @@ */ package org.cyclonedx; +import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import org.cyclonedx.exception.ParseException; import org.cyclonedx.generators.BomGeneratorFactory; @@ -375,6 +376,29 @@ public void testIssue408Regression_jsonToXml() throws Exception { assertTrue(parser.isValid(loadedFile, version)); } + @Test + public void testIssue439Regression_xmlEmptyLicense() throws Exception { + Version version = Version.VERSION_16; + Bom bom = new Bom(); + bom.addComponent(getComponentWithEmptyLicenseChoice()); + + BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom); + String xmlString = generator.toXmlString(); + + assertFalse(xmlString.isEmpty()); + XmlParser parser = new XmlParser(); + assertTrue(parser.isValid(xmlString.getBytes(StandardCharsets.UTF_8))); + } + + private static Component getComponentWithEmptyLicenseChoice() { + Component component = new Component(); + component.setName("xalan"); + component.setType(Component.Type.LIBRARY); + component.setLicenses(new LicenseChoice()); + component.setPurl("pkg:maven/xalan/xalan@2.6.0?type=jar"); + return component; + } + @Test public void schema16_testEvidence() throws Exception { Version version = Version.VERSION_16;