Skip to content

Commit 2ce4322

Browse files
fix(SpdxExpression): Fix license choice for mixed expressions
Recursively get the valid choices for the `left` an `right` parts of `SodxCompoundExpression`s for the `AND` operator. Signed-off-by: Marcel Bochtler <[email protected]>
1 parent d6f1e99 commit 2ce4322

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

utils/spdx/src/main/kotlin/SpdxExpression.kt

+19-14
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,29 @@ class SpdxCompoundExpression(
310310
right.validate(strictness)
311311
}
312312

313-
override fun validChoicesForDnf(): Set<SpdxExpression> =
314-
when (operator) {
315-
SpdxOperator.AND -> setOf(decompose().reduce(SpdxExpression::and))
316-
317-
SpdxOperator.OR -> {
318-
val validChoicesLeft = when (left) {
319-
is SpdxCompoundExpression -> left.validChoicesForDnf()
320-
else -> left.validChoices()
321-
}
313+
override fun validChoicesForDnf(): Set<SpdxExpression> {
314+
val validChoicesLeft = when (left) {
315+
is SpdxCompoundExpression -> left.validChoicesForDnf()
316+
else -> left.validChoices()
317+
}
322318

323-
val validChoicesRight = when (right) {
324-
is SpdxCompoundExpression -> right.validChoicesForDnf()
325-
else -> right.validChoices()
326-
}
319+
val validChoicesRight = when (right) {
320+
is SpdxCompoundExpression -> right.validChoicesForDnf()
321+
else -> right.validChoices()
322+
}
327323

328-
validChoicesLeft + validChoicesRight
324+
return when (operator) {
325+
SpdxOperator.AND -> {
326+
validChoicesLeft.map { leftChoice ->
327+
validChoicesRight.map { rightChoice ->
328+
if (leftChoice == rightChoice) leftChoice else leftChoice and rightChoice
329+
}
330+
}.flatten().toSet()
329331
}
332+
333+
SpdxOperator.OR -> validChoicesLeft + validChoicesRight
330334
}
335+
}
331336

332337
override fun offersChoice(): Boolean =
333338
when (operator) {

utils/spdx/src/test/kotlin/SpdxExpressionTest.kt

+9
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ class SpdxExpressionTest : WordSpec() {
439439
"not contain duplicate valid choice different left and right expressions" {
440440
"a AND a AND b".toSpdx().validChoices() should containExactly("a AND b".toSpdx())
441441
}
442+
443+
"return the correct choices for a mixed expression" {
444+
val spdxExpression = "a AND (a OR b)".toSpdx()
445+
446+
spdxExpression.validChoices() should containExactlyInAnyOrder(
447+
"a".toSpdx(),
448+
"a AND b".toSpdx()
449+
)
450+
}
442451
}
443452

444453
"offersChoice()" should {

0 commit comments

Comments
 (0)