Skip to content

Commit

Permalink
fix(node): Deserialize repository: {} in package.json to null
Browse files Browse the repository at this point in the history
A repository node without a `url` does not make any sense. So, discard
such nodes to not throw a `MissingFieldException` while keeping
`Repository.url` non-nullable.

Fixes #9378.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Nov 6, 2024
1 parent 241da93 commit 9c22891
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugins/package-managers/node/src/main/kotlin/PackageJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ private fun transformPackageJson(element: JsonElement): JsonElement {
val entries = obj.entries.associateTo(mutableMapOf()) { it.toPair() }.apply {
remove("license")
put("licenses", JsonArray(licenses.map { JsonPrimitive(it) }))

(obj["repository"] as? JsonObject)?.let { repository ->
// A repository object node without a `url` does not make sense. However, some packages use 'repository: {}'
// to describe the absence of a repository, see https://github.com/oss-review-toolkit/ort/issues/9378.
// Remove the repository node, so that Repository.url can remain non-nullable.
if ("url" !in repository.keys) remove("repository")
}
}

return JsonObject(entries)
Expand Down
13 changes: 13 additions & 0 deletions plugins/package-managers/node/src/test/kotlin/PackageJsonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package org.ossreviewtoolkit.plugins.packagemanagers.node
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.containExactlyInAnyOrder
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe

class PackageJsonTest : WordSpec({
"parsePackageJson()" should {
Expand Down Expand Up @@ -68,5 +69,17 @@ class PackageJsonTest : WordSpec({
"John Doe"
)
}

"deserialize the repository from an empty node" {
val json = """
{
"repository": {}
}
""".trimIndent()

val packageJson = parsePackageJson(json)

packageJson.repository shouldBe null
}
}
})

0 comments on commit 9c22891

Please sign in to comment.