Skip to content

Commit 3f835b3

Browse files
committed
refactor(cargo): Migrate manifest parsing to kotlinx-serialization
Note that kotlinx-serialization requires optional properties to have default values even if they are nullable. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 44523e4 commit 3f835b3

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

plugins/package-managers/cargo/build.gradle.kts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
21+
2022
plugins {
2123
// Apply precompiled plugins.
2224
id("ort-library-conventions")
@@ -40,10 +42,18 @@ dependencies {
4042
implementation(project(":utils:ort-utils"))
4143
implementation(project(":utils:spdx-utils"))
4244

43-
implementation(libs.jacksonDatabind)
44-
implementation(libs.jacksonModuleKotlin)
45-
implementation(libs.kotlinxSerializationCore)
45+
implementation(libs.bundles.kotlinxSerialization)
4646
implementation(libs.tomlkt)
4747

4848
funTestImplementation(testFixtures(project(":analyzer")))
4949
}
50+
51+
tasks.withType<KotlinCompile>().configureEach {
52+
val customCompilerArgs = listOf(
53+
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
54+
)
55+
56+
compilerOptions {
57+
freeCompilerArgs.addAll(customCompilerArgs)
58+
}
59+
}

plugins/package-managers/cargo/src/main/kotlin/Cargo.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121

2222
package org.ossreviewtoolkit.plugins.packagemanagers.cargo
2323

24-
import com.fasterxml.jackson.module.kotlin.readValue
25-
2624
import java.io.File
2725

26+
import kotlinx.serialization.json.Json
27+
import kotlinx.serialization.json.JsonNamingStrategy
28+
2829
import net.peanuuutz.tomlkt.Toml
2930
import net.peanuuutz.tomlkt.decodeFromNativeReader
3031

@@ -46,7 +47,6 @@ import org.ossreviewtoolkit.model.RemoteArtifact
4647
import org.ossreviewtoolkit.model.Scope
4748
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
4849
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
49-
import org.ossreviewtoolkit.model.jsonMapper
5050
import org.ossreviewtoolkit.model.orEmpty
5151
import org.ossreviewtoolkit.utils.common.CommandLineTool
5252
import org.ossreviewtoolkit.utils.common.splitOnWhitespace
@@ -56,6 +56,11 @@ import org.ossreviewtoolkit.utils.ort.ProcessedDeclaredLicense
5656
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
5757
import org.ossreviewtoolkit.utils.spdx.SpdxOperator
5858

59+
private val json = Json {
60+
ignoreUnknownKeys = true
61+
namingStrategy = JsonNamingStrategy.SnakeCase
62+
}
63+
5964
private val toml = Toml { ignoreUnknownKeys = true }
6065

6166
/**
@@ -164,7 +169,7 @@ class Cargo(
164169

165170
val workingDir = definitionFile.parentFile
166171
val metadataProcess = run(workingDir, "metadata", "--format-version=1")
167-
val metadata = jsonMapper.readValue<CargoMetadata>(metadataProcess.stdout)
172+
val metadata = json.decodeFromString<CargoMetadata>(metadataProcess.stdout)
168173
val hashes = readHashes(resolveLockfile(metadata))
169174

170175
val packages = metadata.packages.associateBy(

plugins/package-managers/cargo/src/main/kotlin/CargoMetadata.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,46 @@
1919

2020
package org.ossreviewtoolkit.plugins.packagemanagers.cargo
2121

22-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
22+
import kotlinx.serialization.Serializable
2323

2424
/**
2525
* See https://doc.rust-lang.org/cargo/commands/cargo-metadata.html.
2626
*/
27-
@JsonIgnoreProperties(ignoreUnknown = true)
27+
@Serializable
2828
internal data class CargoMetadata(
2929
val packages: List<Package>,
3030
val workspaceMembers: List<String>,
3131
val resolve: Resolve,
3232
val workspaceRoot: String
3333
) {
34-
@JsonIgnoreProperties(ignoreUnknown = true)
34+
@Serializable
3535
data class Package(
3636
val name: String,
3737
val version: String,
3838
val id: String,
39-
val license: String?,
40-
val licenseFile: String?,
41-
val description: String?,
42-
val source: String?,
43-
val dependencies: List<Dependency>,
44-
val authors: List<String>,
45-
val repository: String?,
46-
val homepage: String?
39+
val license: String? = null,
40+
val licenseFile: String? = null,
41+
val description: String? = null,
42+
val source: String? = null,
43+
val dependencies: List<Dependency> = emptyList(),
44+
val authors: List<String> = emptyList(),
45+
val repository: String? = null,
46+
val homepage: String? = null
4747
)
4848

49-
@JsonIgnoreProperties(ignoreUnknown = true)
49+
@Serializable
5050
data class Dependency(
5151
val name: String,
52-
val kind: String?
52+
val kind: String? = null
5353
)
5454

55+
@Serializable
5556
data class Resolve(
5657
val nodes: List<Node>,
57-
val root: String?
58+
val root: String? = null
5859
)
5960

60-
@JsonIgnoreProperties(ignoreUnknown = true)
61+
@Serializable
6162
data class Node(
6263
val id: String,
6364
val dependencies: List<String>

0 commit comments

Comments
 (0)