Skip to content

Commit 98caf3f

Browse files
committed
feat(node): Tolerate authors with only an email address
Try to reconstruct the mandatory name from the email address via some simple heuristic. This allows to parse packages like [1]. [1]: https://github.com/rehookify/datepicker/blob/a23eebcc7a2a1595fa72b4725073faae325ac446/package.json#L36 Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 43e1070 commit 98caf3f

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

plugins/package-managers/node/src/main/kotlin/PackageJson.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,20 @@ private object AuthorListSerializer : JsonTransformingSerializer<List<Author>>(s
186186
is JsonObject -> listOf(this)
187187

188188
is JsonPrimitive -> {
189-
parseAuthorString(contentOrNull)
190-
.filter { it.name != null }
191-
.map { Author(checkNotNull(it.name), it.email, it.homepage) }
192-
.map { JSON.encodeToJsonElement(it) }
189+
parseAuthorString(contentOrNull).mapNotNull { info ->
190+
when {
191+
info.name != null -> Author(checkNotNull(info.name), info.email, info.homepage)
192+
info.email == null -> null
193+
else -> {
194+
val nameFromEmail = checkNotNull(info.email).substringBefore('@')
195+
.replace('.', ' ')
196+
.replace(Regex("\\b([a-z])")) { it.value.uppercase() }
197+
Author(nameFromEmail, info.email, info.homepage)
198+
}
199+
}
200+
}.map {
201+
JSON.encodeToJsonElement(it)
202+
}
193203
}
194204

195205
else -> throw SerializationException("Unexpected JSON element.")

plugins/package-managers/node/src/test/kotlin/PackageJsonTest.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import org.ossreviewtoolkit.plugins.packagemanagers.node.PackageJson.Author
2929

3030
class PackageJsonTest : WordSpec({
3131
"parsePackageJson()" should {
32-
"deserialize the author from a textual node" {
32+
"deserialize the author from a primitive node with name and email" {
3333
val json = """
3434
{
3535
"author": "Jane Doe <[email protected]>"
@@ -43,6 +43,20 @@ class PackageJsonTest : WordSpec({
4343
)
4444
}
4545

46+
"deserialize the author from a primitive node with email only" {
47+
val json = """
48+
{
49+
"author": "<[email protected]>"
50+
}
51+
""".trimIndent()
52+
53+
val packageJson = parsePackageJson(json)
54+
55+
packageJson.authors should containExactlyInAnyOrder(
56+
Author(name = "Jane Doe", email = "[email protected]")
57+
)
58+
}
59+
4660
"deserialize the author from an object node" {
4761
val json = """
4862
{

0 commit comments

Comments
 (0)