From 72d929155da0d378537268a78465932c99acd265 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 9 Jul 2024 13:03:14 +0200 Subject: [PATCH] fix(nuget): Parse namespaces for names that include versions correctly Refactor the code to make it easier to check for name(space) components if they start with a digit, and if so, treat all components to be part of the name and use an empty namespace. This fixes parsing of package names that contain a version in addition to the version field. Signed-off-by: Sebastian Schuberth --- .../nuget/src/main/kotlin/utils/NuGetUtils.kt | 20 ++++++++++++++++--- .../src/test/kotlin/utils/NuGetUtilsTest.kt | 5 +++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/plugins/package-managers/nuget/src/main/kotlin/utils/NuGetUtils.kt b/plugins/package-managers/nuget/src/main/kotlin/utils/NuGetUtils.kt index 5d99d5966b24d..20fd37470ec40 100644 --- a/plugins/package-managers/nuget/src/main/kotlin/utils/NuGetUtils.kt +++ b/plugins/package-managers/nuget/src/main/kotlin/utils/NuGetUtils.kt @@ -27,8 +27,22 @@ import org.ossreviewtoolkit.model.Identifier * a NuGet package [Identifier], e.g. to use it as part of curations. */ fun getIdentifierWithNamespace(type: String, name: String, version: String): Identifier { - val namespace = name.split('.', limit = 3).toMutableList() - val nameWithoutNamespace = namespace.removeLast() - val namespaceWithoutName = namespace.joinToString(".") + val components = name.split('.') + val maxNamespaceComponents = (components.size - 1).coerceAtMost(2) + + var i = 0 + while (i < maxNamespaceComponents) { + // If any namespace component start with a digit, treat all components as parts of the name. + if (components[i].first().isDigit()) { + i = 0 + break + } + + ++i + } + + val namespaceWithoutName = components.subList(0, i).joinToString(".") + val nameWithoutNamespace = components.subList(i, components.size).joinToString(".") + return Identifier(type, namespaceWithoutName, nameWithoutNamespace, version) } diff --git a/plugins/package-managers/nuget/src/test/kotlin/utils/NuGetUtilsTest.kt b/plugins/package-managers/nuget/src/test/kotlin/utils/NuGetUtilsTest.kt index ea99e62e50a6f..9c70f352d0cc6 100644 --- a/plugins/package-managers/nuget/src/test/kotlin/utils/NuGetUtilsTest.kt +++ b/plugins/package-managers/nuget/src/test/kotlin/utils/NuGetUtilsTest.kt @@ -57,5 +57,10 @@ class NuGetUtilsTest : WordSpec({ "NuGet:Microsoft.AspNetCore:Components.WebAssembly.Build.BrotliCompression:3.1.6" ) } + + "not use numeric components for the namespace" { + getIdentifierWithNamespace("NuGet", "SharpSvn.1.9-x64", "1.9007.3987.251") shouldBe + Identifier("NuGet::SharpSvn.1.9-x64:1.9007.3987.251") + } } })