diff --git a/model/src/main/kotlin/LicenseFinding.kt b/model/src/main/kotlin/LicenseFinding.kt index 66ed1167fee2c..98485e76a7201 100644 --- a/model/src/main/kotlin/LicenseFinding.kt +++ b/model/src/main/kotlin/LicenseFinding.kt @@ -19,11 +19,16 @@ package com.here.ort.model -import com.fasterxml.jackson.annotation.JsonCreator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.deser.std.StdDeserializer +import com.fasterxml.jackson.module.kotlin.treeToValue import com.here.ort.model.config.CopyrightGarbage import com.here.ort.utils.CopyrightStatementsProcessor import com.here.ort.utils.SortedSetComparator +import com.here.ort.utils.textValueOrEmpty import java.util.SortedMap import java.util.SortedSet @@ -51,7 +56,7 @@ fun LicenseFindingsMap.removeGarbage(copyrightGarbage: CopyrightGarbage) = * A class to store a [license] finding along with its belonging [copyrights]. To support deserializing older versions * of this class which did not include the copyrights a secondary constructor is only taking a [licenseName]. */ -data class LicenseFinding @JsonCreator constructor( +data class LicenseFinding( val license: String, val copyrights: SortedSet ) : Comparable { @@ -59,12 +64,26 @@ data class LicenseFinding @JsonCreator constructor( private val COPYRIGHTS_COMPARATOR = SortedSetComparator() } - @JsonCreator - constructor(licenseName: String) : this(licenseName, sortedSetOf()) - override fun compareTo(other: LicenseFinding) = when { license != other.license -> license.compareTo(other.license) else -> COPYRIGHTS_COMPARATOR.compare(copyrights, other.copyrights) } } + +/** + * Custom deserializer to support old versions of the [LicenseFinding] class. + */ +class LicenseFindingDeserializer : StdDeserializer(LicenseFinding::class.java) { + override fun deserialize(p: JsonParser, ctxt: DeserializationContext): LicenseFinding { + val node = p.codec.readTree(p) + return when { + node.isTextual -> LicenseFinding(node.textValueOrEmpty(), sortedSetOf()) + else -> { + val license = jsonMapper.treeToValue(node["license"]) + val copyrights = jsonMapper.treeToValue>(node["copyrights"]) + return LicenseFinding(license, copyrights) + } + } + } +} diff --git a/model/src/main/kotlin/Mappers.kt b/model/src/main/kotlin/Mappers.kt index 14cd55874a09d..a16e14ccac1d5 100644 --- a/model/src/main/kotlin/Mappers.kt +++ b/model/src/main/kotlin/Mappers.kt @@ -34,6 +34,7 @@ import com.here.ort.model.config.AnalyzerConfigurationDeserializer private val ortModelModule = SimpleModule("OrtModelModule").apply { addDeserializer(AnalyzerConfiguration::class.java, AnalyzerConfigurationDeserializer()) + addDeserializer(LicenseFinding::class.java, LicenseFindingDeserializer()) addDeserializer(OrtIssue::class.java, OrtIssueDeserializer()) addDeserializer(VcsInfo::class.java, VcsInfoDeserializer()) diff --git a/scanner/src/main/kotlin/scanners/Askalono.kt b/scanner/src/main/kotlin/scanners/Askalono.kt index 490b387103cc6..90983c37a4d1b 100644 --- a/scanner/src/main/kotlin/scanners/Askalono.kt +++ b/scanner/src/main/kotlin/scanners/Askalono.kt @@ -157,7 +157,7 @@ class Askalono(config: ScannerConfiguration) : LocalScanner(config) { } override fun generateSummary(startTime: Instant, endTime: Instant, result: JsonNode): ScanSummary { - val findings = result.map { LicenseFinding(it["License"].textValue()) }.toSortedSet() + val findings = result.map { LicenseFinding(it["License"].textValue(), sortedSetOf()) }.toSortedSet() return ScanSummary(startTime, endTime, result.size(), findings, errors = mutableListOf()) } } diff --git a/scanner/src/main/kotlin/scanners/BoyterLc.kt b/scanner/src/main/kotlin/scanners/BoyterLc.kt index c60b67e8e8c2c..3bd7c6c9cd669 100644 --- a/scanner/src/main/kotlin/scanners/BoyterLc.kt +++ b/scanner/src/main/kotlin/scanners/BoyterLc.kt @@ -167,7 +167,7 @@ class BoyterLc(config: ScannerConfiguration) : LocalScanner(config) { result.forEach { file -> file["LicenseGuesses"].mapTo(findings) { license -> - LicenseFinding(license["LicenseId"].textValue()) + LicenseFinding(license["LicenseId"].textValue(), sortedSetOf()) } } diff --git a/scanner/src/main/kotlin/scanners/Licensee.kt b/scanner/src/main/kotlin/scanners/Licensee.kt index 4f2d6e51a17ac..3eb7ce501e913 100644 --- a/scanner/src/main/kotlin/scanners/Licensee.kt +++ b/scanner/src/main/kotlin/scanners/Licensee.kt @@ -138,7 +138,7 @@ class Licensee(config: ScannerConfiguration) : LocalScanner(config) { val matchedFiles = result["matched_files"] val findings = matchedFiles.map { - LicenseFinding(it["matched_license"].textValue()) + LicenseFinding(it["matched_license"].textValue(), sortedSetOf()) }.toSortedSet() return ScanSummary(startTime, endTime, matchedFiles.count(), findings, errors = mutableListOf())