Skip to content

Commit

Permalink
Merge pull request #414 from CycloneDX/issue_408_extensibleTypes
Browse files Browse the repository at this point in the history
Add extensible types during license serialization
  • Loading branch information
stevespringett authored May 30, 2024
2 parents 271e31c + a9ce1dd commit bb0ae15
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ public void serialize(

if (isXml && gen instanceof ToXmlGenerator) {
ToXmlGenerator toXmlGenerator = (ToXmlGenerator) gen;
serializeXml(toXmlGenerator, licenseChoice);
serializeXml(toXmlGenerator, licenseChoice, provider);
}
else {
serializeJson(licenseChoice, gen, provider);
}
}

private void serializeXml(ToXmlGenerator toXmlGenerator, LicenseChoice lc) throws IOException {
private void serializeXml(ToXmlGenerator toXmlGenerator, LicenseChoice lc, final SerializerProvider provider)
throws IOException
{
if (CollectionUtils.isNotEmpty(lc.getLicenses())) {
toXmlGenerator.writeStartObject();
toXmlGenerator.writeFieldName("license");
Expand Down Expand Up @@ -106,6 +108,11 @@ else if (StringUtils.isNotBlank(l.getName())) {
toXmlGenerator.writeEndObject();
}

//It might have extensible types
if(CollectionUtils.isNotEmpty(l.getExtensibleTypes())) {
new ExtensibleTypesSerializer().serialize(l.getExtensibleTypes(), toXmlGenerator, provider);
}

toXmlGenerator.writeEndObject();
}
toXmlGenerator.writeEndArray();
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/cyclonedx/BomXmlGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import java.util.Objects;

Expand Down Expand Up @@ -507,6 +509,30 @@ public void testIssue408Regression_externalReferenceBom() throws Exception {
assertTrue(parser.isValid(loadedFile, version));
}

@Test
public void testIssue408Regression_extensibleTypes() throws Exception {
Version version = Version.VERSION_15;
Bom bom = createCommonBomXml("/regression/issue408-extensible-type.xml");
addExtensibleTypes(bom);

BomXmlGenerator generator = BomGeneratorFactory.createXml(version, bom);
File loadedFile = writeToFile(generator.toXmlString());

XmlParser parser = new XmlParser();
assertTrue(parser.isValid(loadedFile, version));
}

private void addExtensibleTypes(Bom bom) {
ExtensibleType t1 = new ExtensibleType("abc", "test", "test");
ExtensibleType t2 = new ExtensibleType("abc", "test", "test1");

bom.getComponents().get(0).getLicenses().getLicenses().get(0).addExtensibleType(t1);
bom.getComponents().get(0).getLicenses().getLicenses().get(1).addExtensibleType(t2);

ExtensibleType t3 = new ExtensibleType("abc", "info", "test");
bom.getComponents().get(0).addExtensibleType(t3);
}

@Test
public void testIssue408Regression_jsonToXml_externalReferenceBom() throws Exception {
Version version = Version.VERSION_16;
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/regression/issue408-extensible-type.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<bom serialNumber="urn:uuid:7fec5e81-b204-4eec-a0d6-383dc39c4867" version="1" xmlns="http://cyclonedx.org/schema/bom/1.5">
<components>
<component type="library" bom-ref="NPM:@ort:concluded-license:1.0">
<name>concluded-license</name>
<version>1.0</version>
<licenses>
<license>
<id>MIT</id>
<abc:test xmlns:abc="http://www.w3.org/1999/xhtml">test</abc:test>
</license>
<license>
<name>MIT WITH Libtool-exception</name>
<abc:test xmlns:abc="http://www.w3.org/1999/xhtml">test1</abc:test>
</license>
</licenses>
<purl>pkg:npm/%40ort/[email protected]?classifier=sources</purl>
<abc:info xmlns:abc="http://www.w3.org/1999/xhtml">test</abc:info>
</component>
</components>
</bom>

0 comments on commit bb0ae15

Please sign in to comment.