Skip to content

Commit da2f8a1

Browse files
tiegzgoneall
authored andcommitted
Also convert orphan extracted/listed licenses and exceptions as root elements
Extracted licensing info, listed licenses, and listed license exceptions that exist in a source document's model store but are not reachable from documentDescribes, a relationship, or any referenced license expression were previously dropped from the SPDX 3 output, since they were never added to the document's root elements. - Add extracted licenses to the root elements list directly, since they are already unconditionally converted from the document's extractedLicenseInfos. - Scan the source model store for listed licenses and listed license exceptions not already converted, converting and rooting any orphans found (searched without a document URI prefix since these are stored under the SPDX License List namespace).
1 parent a7b5e16 commit da2f8a1

2 files changed

Lines changed: 118 additions & 8 deletions

File tree

‎src/main/java/org/spdx/library/conversion/Spdx2to3Converter.java‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -700,17 +700,18 @@ public SpdxDocument convertAndStore(org.spdx.library.model.v2.SpdxDocument fromD
700700
}
701701
).collect(Collectors.toList()));
702702
for (org.spdx.library.model.v2.license.ExtractedLicenseInfo extractedLicense:fromDoc.getExtractedLicenseInfos()) {
703-
convertAndStore(extractedLicense);
703+
toDoc.getRootElements().add(convertAndStore(extractedLicense));
704704
}
705705
convertOrphanElements(fromDoc, toDoc);
706706
return toDoc;
707707
}
708708

709709
/**
710-
* Converts any SPDX spec version 2 packages, files, and snippets belonging to fromDoc which are not
711-
* reachable through documentDescribes or a relationship (i.e. have no edges connecting them to anything
712-
* else in the document). Without this, such elements would be silently dropped from the conversion since
713-
* they would never be visited by the relationship graph traversal performed elsewhere in this class.
710+
* Converts any SPDX spec version 2 packages, files, snippets, listed licenses, and listed license
711+
* exceptions belonging to fromDoc which are not reachable through documentDescribes, a relationship,
712+
* or a license expression (i.e. have no edges connecting them to anything else in the document).
713+
* Without this, such elements would be silently dropped from the conversion since they would never
714+
* be visited by the relationship graph traversal performed elsewhere in this class.
714715
* Any such elements found are converted, stored, and added to the toDoc's root elements.
715716
* @param fromDoc SPDX spec version 2 document being converted from
716717
* @param toDoc SPDX spec version 3 document being converted to
@@ -731,6 +732,24 @@ private void convertOrphanElements(org.spdx.library.model.v2.SpdxDocument fromDo
731732
}
732733
}
733734
}
735+
// Listed licenses and listed license exceptions are stored under the SPDX License List namespace
736+
// rather than the document's namespace, so no object URI prefix filter is used to find them.
737+
List<org.spdx.library.model.v2.license.SpdxListedLicense> fromListedLicenses = ((Stream<org.spdx.library.model.v2.license.SpdxListedLicense>)
738+
SpdxModelFactory.getSpdxObjects(fromModelStore, copyManager, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE, null, null))
739+
.collect(Collectors.toList());
740+
for (org.spdx.library.model.v2.license.SpdxListedLicense fromListedLicense : fromListedLicenses) {
741+
if (!alreadyCopied(fromListedLicense.getObjectUri())) {
742+
toDoc.getRootElements().add(convertAndStore(fromListedLicense));
743+
}
744+
}
745+
List<org.spdx.library.model.v2.license.ListedLicenseException> fromListedExceptions = ((Stream<org.spdx.library.model.v2.license.ListedLicenseException>)
746+
SpdxModelFactory.getSpdxObjects(fromModelStore, copyManager, SpdxConstantsCompatV2.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null, null))
747+
.collect(Collectors.toList());
748+
for (org.spdx.library.model.v2.license.ListedLicenseException fromListedException : fromListedExceptions) {
749+
if (!alreadyCopied(fromListedException.getObjectUri())) {
750+
toDoc.getRootElements().add(convertAndStore(fromListedException));
751+
}
752+
}
734753
}
735754

736755
/**

‎src/test/java/org/spdx/library/conversion/Spdx2to3ConverterTest.java‎

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,21 @@ public void testConvertAndStoreSpdxDocument() throws InvalidSPDXAnalysisExceptio
608608
assertEquals(annotationComment, resultAnnotation.getStatement().get());
609609

610610
Element[] rootElements = result.getRootElements().toArray(new Element[result.getRootElements().size()]);
611-
assertEquals(1, rootElements.length);
612-
assertTrue(rootElements[0] instanceof SpdxPackage);
613-
SpdxPackage resultPkg = (SpdxPackage)rootElements[0];
611+
assertEquals(2, rootElements.length);
612+
SpdxPackage resultPkg = null;
613+
CustomLicense rootCustomLicense = null;
614+
for (Element rootElement:rootElements) {
615+
if (rootElement instanceof SpdxPackage) {
616+
resultPkg = (SpdxPackage)rootElement;
617+
} else if (rootElement instanceof CustomLicense) {
618+
rootCustomLicense = (CustomLicense)rootElement;
619+
} else {
620+
fail("Unexpected root element type "+rootElement.getClass().getName());
621+
}
622+
}
623+
assertTrue(Objects.nonNull(resultPkg));
624+
assertTrue(Objects.nonNull(rootCustomLicense));
625+
assertEquals(extractedLicName, rootCustomLicense.getName().get());
614626
assertEquals(pkgName, resultPkg.getName().get());
615627
assertEquals(pkgDownloadLocation, resultPkg.getDownloadLocation().get());
616628
assertEquals(pkgCopyright, resultPkg.getCopyrightText().get());
@@ -722,6 +734,85 @@ public void testConvertAndStoreSpdxDocumentOrphanPackage() throws InvalidSPDXAna
722734
assertTrue(resultPackageNames.contains(describedPkgName));
723735
assertTrue(resultPackageNames.contains(orphanPkgName));
724736
}
737+
738+
/**
739+
* Test method for {@link org.spdx.library.conversion.Spdx2to3Converter#convertAndStore(org.spdx.library.model.v2.SpdxDocument)}.
740+
* Verifies that extracted licensing info, listed licenses, and listed license exceptions stored in the
741+
* document's model store but not referenced by any package, file, or snippet are still converted and
742+
* added as root elements rather than silently dropped.
743+
* @throws InvalidSPDXAnalysisException
744+
*/
745+
@Test
746+
public void testConvertAndStoreSpdxDocumentOrphanLicenses() throws InvalidSPDXAnalysisException {
747+
String pkgName = "package";
748+
String extractedLicName = "Extracted License Name";
749+
String extractedLicText = "Extracted license text";
750+
String orphanLicenseId = "Apache-2.0";
751+
String orphanExceptionId = "Classpath-exception-2.0";
752+
753+
org.spdx.library.model.v2.SpdxDocument doc = new org.spdx.library.model.v2.SpdxDocument(fromModelStore, DOCUMENT_URI, copyManager, true);
754+
doc.setCreationInfo(doc.createCreationInfo(Arrays.asList(new String[] {SpdxConstantsCompatV2.CREATOR_PREFIX_TOOL + "test"}),
755+
"2010-01-29T18:30:22Z"));
756+
org.spdx.library.model.v2.license.AnyLicenseInfo dataLicense =
757+
LicenseInfoFactory.parseSPDXLicenseStringCompatV2("CC0-1.0", fromModelStore, DOCUMENT_URI, copyManager);
758+
doc.setDataLicense(dataLicense);
759+
760+
org.spdx.library.model.v2.license.AnyLicenseInfo noAssertion =
761+
new org.spdx.library.model.v2.license.SpdxNoAssertionLicense();
762+
org.spdx.library.model.v2.SpdxPackage pkg = doc.createPackage(fromModelStore.getNextId(IdType.SpdxId),
763+
pkgName, noAssertion, "copyright", noAssertion)
764+
.setFilesAnalyzed(false)
765+
.setDownloadLocation("NOASSERTION")
766+
.build();
767+
doc.setDocumentDescribes(Arrays.asList(new org.spdx.library.model.v2.SpdxItem[] {pkg}));
768+
769+
// extracted license info stored at the document level but not referenced by the package
770+
org.spdx.library.model.v2.license.ExtractedLicenseInfo extractedLicense =
771+
new org.spdx.library.model.v2.license.ExtractedLicenseInfo(fromModelStore, DOCUMENT_URI,
772+
fromModelStore.getNextId(IdType.LicenseRef), copyManager, true);
773+
extractedLicense.setExtractedText(extractedLicText);
774+
extractedLicense.setName(extractedLicName);
775+
doc.setExtractedLicenseInfos(Arrays.asList(new org.spdx.library.model.v2.license.ExtractedLicenseInfo[] {extractedLicense}));
776+
777+
// listed license and listed license exception stored in the document's model store but not
778+
// referenced by the package, a relationship, or documentDescribes
779+
org.spdx.library.model.v2.license.SpdxListedLicense orphanLicense =
780+
new org.spdx.library.model.v2.license.SpdxListedLicense(fromModelStore,
781+
SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, orphanLicenseId, copyManager, true);
782+
org.spdx.library.model.v2.license.ListedLicenseException orphanException =
783+
new org.spdx.library.model.v2.license.ListedLicenseException(fromModelStore,
784+
SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX, orphanExceptionId, copyManager, true);
785+
786+
Spdx2to3Converter converter = new Spdx2to3Converter(toModelStore, copyManager, defaultCreationInfo,
787+
SpdxModelFactory.getLatestSpecVersion(), DEFAULT_PREFIX, true);
788+
SpdxDocument result = converter.convertAndStore(doc);
789+
790+
Element[] rootElements = result.getRootElements().toArray(new Element[result.getRootElements().size()]);
791+
assertEquals(4, rootElements.length);
792+
boolean foundPkg = false;
793+
boolean foundCustomLicense = false;
794+
boolean foundListedLicense = false;
795+
boolean foundListedException = false;
796+
for (Element rootElement:rootElements) {
797+
if (rootElement instanceof SpdxPackage) {
798+
foundPkg = true;
799+
assertEquals(pkgName, ((SpdxPackage)rootElement).getName().get());
800+
} else if (rootElement instanceof CustomLicense) {
801+
foundCustomLicense = true;
802+
assertEquals(extractedLicName, ((CustomLicense)rootElement).getName().get());
803+
} else if (rootElement instanceof ListedLicense) {
804+
foundListedLicense = true;
805+
} else if (rootElement instanceof ListedLicenseException) {
806+
foundListedException = true;
807+
} else {
808+
fail("Unexpected root element type "+rootElement.getClass().getName());
809+
}
810+
}
811+
assertTrue(foundPkg);
812+
assertTrue(foundCustomLicense);
813+
assertTrue(foundListedLicense);
814+
assertTrue(foundListedException);
815+
}
725816

726817
@Test
727818
public void testConvertAndStoreExternalDocRef() throws InvalidSPDXAnalysisException {

0 commit comments

Comments
 (0)