Skip to content

Commit 30e362c

Browse files
committed
feat(reporter): Improve handling of unmapped licenses in SPDX reporter
If a package has unmapped declared licenses, always append `NOASSERTION` to the declared license SPDX expression. Previously, `NOASSERTION` was only added if the SPDX expression was null or blank. This did hide the fact that there are unmapped licenses if the expression was not empty. Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.io>
1 parent a05529d commit 30e362c

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

reporter/src/funTest/assets/spdx-document-reporter-expected-output.spdx.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"filesAnalyzed" : false,
9797
"homepage" : "NONE",
9898
"licenseConcluded" : "NOASSERTION",
99-
"licenseDeclared" : "MIT",
99+
"licenseDeclared" : "MIT AND NOASSERTION",
100100
"name" : "fourth-package",
101101
"summary" : "A package with partially mapped declared license.",
102102
"versionInfo" : "0.0.1"

reporter/src/funTest/assets/spdx-document-reporter-expected-output.spdx.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ packages:
108108
filesAnalyzed: false
109109
homepage: "NONE"
110110
licenseConcluded: "NOASSERTION"
111-
licenseDeclared: "MIT"
111+
licenseDeclared: "MIT AND NOASSERTION"
112112
name: "fourth-package"
113113
summary: "A package with partially mapped declared license."
114114
versionInfo: "0.0.1"

reporter/src/main/kotlin/reporters/spdx/SpdxDocumentModelMapper.kt

+13-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import org.ossreviewtoolkit.utils.spdx.model.SpdxExtractedLicenseInfo
4949
import org.ossreviewtoolkit.utils.spdx.model.SpdxPackage
5050
import org.ossreviewtoolkit.utils.spdx.model.SpdxPackageVerificationCode
5151
import org.ossreviewtoolkit.utils.spdx.model.SpdxRelationship
52+
import org.ossreviewtoolkit.utils.spdx.toSpdx
5253
import org.ossreviewtoolkit.utils.spdx.toSpdxId
5354

5455
/**
@@ -242,11 +243,18 @@ private fun Package.toSpdxPackage(licenseInfoResolver: LicenseInfoResolver, isPr
242243

243244
private fun ProcessedDeclaredLicense.toSpdxDeclaredLicense(): String =
244245
when {
245-
unmapped.isEmpty() -> spdxExpression.nullOrBlankToSpdxNoassertionOrNone()
246-
spdxExpression == null -> SpdxConstants.NOASSERTION
247-
spdxExpression.toString().isBlank() -> SpdxConstants.NOASSERTION
248-
spdxExpression.toString() == SpdxConstants.NONE -> SpdxConstants.NOASSERTION
249-
else -> spdxExpression.toString()
246+
// If there are unmapped licenses, represent this by adding NOASSERTION.
247+
unmapped.isNotEmpty() -> {
248+
spdxExpression?.let {
249+
if (SpdxConstants.NOASSERTION !in it.licenses()) {
250+
(it and SpdxConstants.NOASSERTION.toSpdx()).toString()
251+
} else {
252+
it.toString()
253+
}
254+
} ?: SpdxConstants.NOASSERTION
255+
}
256+
257+
else -> spdxExpression.nullOrBlankToSpdxNoassertionOrNone()
250258
}
251259

252260
private fun String?.nullOrBlankToSpdxNone(): String = if (isNullOrBlank()) SpdxConstants.NONE else this

0 commit comments

Comments
 (0)