Skip to content

Commit 2ccef49

Browse files
committed
chore(scanner): Partly avoid the need for custom serial formats
If the only customization is to ignore unknown keys, prefer to use the class annotation instead, which nicely documents the class to be potentially incomplete as not all properties are needed, and avoids the need for a custom serial format instance. While doing this, review for which classes ignoring unknown keys is actually required, and add links to the upstream data models. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 02a530d commit 2ccef49

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

plugins/scanners/askalono/src/main/kotlin/Askalono.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ import org.ossreviewtoolkit.utils.common.Os
4545

4646
private const val CONFIDENCE_NOTICE = "Confidence threshold not high enough for any known license"
4747

48-
private val JSON = Json { ignoreUnknownKeys = true }
49-
5048
object AskalonoCommand : CommandLineTool {
5149
override fun command(workingDir: File?) =
5250
listOfNotNull(workingDir, if (Os.isWindows) "askalono.exe" else "askalono").joinToString(File.separator)
@@ -107,7 +105,7 @@ class Askalono(
107105
}
108106

109107
override fun createSummary(result: String, startTime: Instant, endTime: Instant): ScanSummary {
110-
val results = result.byteInputStream().use { JSON.decodeToSequence<AskalonoResult>(it) }
108+
val results = result.byteInputStream().use { Json.decodeToSequence<AskalonoResult>(it) }
111109

112110
val licenseFindings = mutableSetOf<LicenseFinding>()
113111

plugins/scanners/askalono/src/main/kotlin/AskalonoResultModel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,27 @@
2020
package org.ossreviewtoolkit.plugins.scanners.askalono
2121

2222
import kotlinx.serialization.Serializable
23+
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
2324

25+
// See https://github.com/jpeddicord/askalono/blob/0.5.0/cli/src/formats.rs#L12-L23.
2426
@Serializable
2527
data class AskalonoResult(
2628
val path: String,
2729
val result: PathResult? = null,
2830
val error: String? = null
2931
)
3032

33+
// See https://github.com/jpeddicord/askalono/blob/0.5.0/cli/src/formats.rs#L25-L30.
3134
@Serializable
35+
@JsonIgnoreUnknownKeys
3236
data class PathResult(
3337
val score: Float,
3438
val license: LicenseResult
3539
)
3640

41+
// See https://github.com/jpeddicord/askalono/blob/0.5.0/cli/src/formats.rs#L32-L37.
3742
@Serializable
43+
@JsonIgnoreUnknownKeys
3844
data class LicenseResult(
3945
val name: String,
4046
val aliases: Set<String>

plugins/scanners/boyterlc/src/main/kotlin/BoyterLc.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ import org.ossreviewtoolkit.utils.common.Os
4444
import org.ossreviewtoolkit.utils.common.safeDeleteRecursively
4545
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
4646

47-
private val JSON = Json { ignoreUnknownKeys = true }
48-
4947
object BoyterLcCommand : CommandLineTool {
5048
override fun command(workingDir: File?) =
5149
listOfNotNull(workingDir, if (Os.isWindows) "lc.exe" else "lc").joinToString(File.separator)
@@ -115,7 +113,7 @@ class BoyterLc(
115113
}
116114

117115
override fun createSummary(result: String, startTime: Instant, endTime: Instant): ScanSummary {
118-
val results = JSON.decodeFromString<List<BoyterLcResult>>(result)
116+
val results = Json.decodeFromString<List<BoyterLcResult>>(result)
119117

120118
val licenseFindings = results.flatMapTo(mutableSetOf()) {
121119
val filePath = File(it.directory, it.filename)

plugins/scanners/boyterlc/src/main/kotlin/BoyterLcResultModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ package org.ossreviewtoolkit.plugins.scanners.boyterlc
2121

2222
import kotlinx.serialization.SerialName
2323
import kotlinx.serialization.Serializable
24+
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
2425

26+
// See https://github.com/boyter/lc/blob/7a7c5750857efde2f1d50b1c3b62c07943587421/parsers/structs.go#L20-L31.
2527
@Serializable
28+
@JsonIgnoreUnknownKeys
2629
class BoyterLcResult(
2730
@SerialName("Directory") val directory: String,
2831
@SerialName("Filename") val filename: String,
2932
@SerialName("LicenseGuesses") val licenseGuesses: List<LicenseGuess>
3033
)
3134

35+
// See https://github.com/boyter/lc/blob/v1.3.1/parsers/structs.go#L15-L18.
3236
@Serializable
3337
data class LicenseGuess(
3438
@SerialName("LicenseId") val licenseId: String,

0 commit comments

Comments
 (0)