Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/kotlin/no/item/xp/plugin/DuplicateFieldNameException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package no.item.xp.plugin

import org.gradle.api.GradleException

class DuplicateFieldNameException(
val fieldNames: List<String>,
val source: String? = null,
) : GradleException(createMessage(fieldNames, source)) {
/**
* Adds the file the duplicates was found in, unless it has already been added by a more specific caller
*/
fun withSource(source: String): DuplicateFieldNameException =
if (this.source == null) DuplicateFieldNameException(fieldNames, source) else this
}

private fun createMessage(
fieldNames: List<String>,
source: String?,
): String {
val label = if (fieldNames.size == 1) "Duplicate field name" else "Duplicate field names"
val names = fieldNames.joinToString(", ") { "\"$it\"" }
val location = source?.let { " in \"$it\"" } ?: ""

return "$label $names$location. A field name can only be used once in the same object."
}
6 changes: 5 additions & 1 deletion src/main/kotlin/no/item/xp/plugin/GenerateCodeTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ open class GenerateCodeTask
ObjectTypeModel(Paths.get(fileInJar.entry.name).fileName.nameWithoutExtension, emptyList()).right()
},
{
parseObjectTypeModel(it, Paths.get(fileInJar.entry.name).fileName.nameWithoutExtension, mixins)
try {
parseObjectTypeModel(it, Paths.get(fileInJar.entry.name).fileName.nameWithoutExtension, mixins)
} catch (e: DuplicateFieldNameException) {
throw e.withSource(fileInJar.entry.name)
}
},
).fold(
{ left ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ abstract class GenerateTypeScriptWorkAction : WorkAction<CodegenWorkParameters>
logger.lifecycle("Updated file: ${Path.of(targetFile.absoluteFile.toURI()).toUri()}")
},
)
} catch (e: DuplicateFieldNameException) {
throw e.withSource(simpleFilePath(parameters.getXmlFile().get().asFile))
} catch (e: Exception) {
logger.error("Can't parse file", e)
}
Expand Down
37 changes: 27 additions & 10 deletions src/main/kotlin/no/item/xp/plugin/parser/ParseInterfaceModel.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.item.xp.plugin.parser

import arrow.core.Either
import no.item.xp.plugin.DuplicateFieldNameException
import no.item.xp.plugin.extensions.getChildNodesAtXPath
import no.item.xp.plugin.extensions.getChildNodesAtXPathAsEither
import no.item.xp.plugin.extensions.getNodeAttribute
Expand Down Expand Up @@ -37,17 +38,33 @@ fun parseInputTypeList(
nodes: Collection<Node>,
mixins: List<ObjectTypeModel>,
): List<ObjectTypeModelField> {
return nodes
.flatMap { node ->
when (node.nodeName) {
"input" -> listOfNotNull(parseInput(node))
"option-set" -> listOfNotNull(parseOptionSet(node, mixins))
"item-set" -> listOfNotNull(parseItemSet(node, mixins))
"field-set" -> parseFieldSet(node, mixins)
"mixin" -> findMixinFields(mixins, node)
else -> emptyList()
val fields =
nodes
.flatMap { node ->
when (node.nodeName) {
"input" -> listOfNotNull(parseInput(node))
"option-set" -> listOfNotNull(parseOptionSet(node, mixins))
"item-set" -> listOfNotNull(parseItemSet(node, mixins))
"field-set" -> parseFieldSet(node, mixins)
"mixin" -> findMixinFields(mixins, node)
else -> emptyList()
}
}
}

// Fields from mixins and field-sets are added to the same object, so their names can collide
val duplicateFieldNames =
fields
.groupingBy { it.name }
.eachCount()
.filterValues { it > 1 }
.keys
.toList()

if (duplicateFieldNames.isNotEmpty()) {
throw DuplicateFieldNameException(duplicateFieldNames)
}

return fields
}

private fun findMixinFields(
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/no/item/xp/plugin/parser/ParseMixin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.item.xp.plugin.parser
import arrow.core.Either
import arrow.core.flatMap
import no.item.xp.plugin.CyclicDependenciesException
import no.item.xp.plugin.DuplicateFieldNameException
import no.item.xp.plugin.extensions.getChildNodesAtXPath
import no.item.xp.plugin.extensions.getFormNode
import no.item.xp.plugin.models.MixinDependencyModel
Expand Down Expand Up @@ -82,7 +83,11 @@ private fun walkMixinGraph(
interfaceModel
}

return parseObjectTypeModel(mixin.node, mixin.name, dependentOnMixins).getOrNull()
try {
return parseObjectTypeModel(mixin.node, mixin.name, dependentOnMixins).getOrNull()
} catch (e: DuplicateFieldNameException) {
throw e.withSource("site/mixins/${mixin.name}/${mixin.name}.xml")
}
}

fun parseMixinDependencyModel(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package no.item.xp.plugin.parser

import no.item.xp.plugin.DuplicateFieldNameException
import no.item.xp.plugin.extensions.getChildNodeAtXPath
import no.item.xp.plugin.models.ObjectTypeModel
import no.item.xp.plugin.models.StringField
import no.item.xp.plugin.stringToXMLDocument
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ParseDuplicateFieldNameTest {
private val mixins = listOf(ObjectTypeModel("intro", listOf(StringField("intro", "Intro", true, false))))

private fun parseForm(xml: String) =
parseObjectTypeModel(stringToXMLDocument(xml).getChildNodeAtXPath("content-type/form")!!, "article", mixins)

@Test
fun `fail if mixin contains field with same name`() {
// language=XML
val xml =
"""
<content-type>
<form>
<input name="intro" type="TextLine">
<label>Intro</label>
</input>
<mixin name="intro"/>
</form>
</content-type>
"""

val exception = assertFailsWith<DuplicateFieldNameException> { parseForm(xml) }

assertEquals(listOf("intro"), exception.fieldNames)
}

@Test
fun `fail if field-set contains field with same name`() {
// language=XML
val xml =
"""
<content-type>
<form>
<item-set name="block">
<items>
<input name="title" type="TextLine"/>
<field-set>
<items>
<input name="title" type="TextArea"/>
</items>
</field-set>
</items>
</item-set>
</form>
</content-type>
"""

val exception = assertFailsWith<DuplicateFieldNameException> { parseForm(xml) }

assertEquals(listOf("title"), exception.fieldNames)
}

@Test
fun `allow same field name in different objects`() {
// language=XML
val xml =
"""
<content-type>
<form>
<input name="title" type="TextLine"/>
<item-set name="block">
<items>
<input name="title" type="TextLine"/>
</items>
</item-set>
<option-set name="link">
<options minimum="1" maximum="1">
<option name="internal">
<items>
<input name="title" type="TextLine"/>
</items>
</option>
</options>
</option-set>
</form>
</content-type>
"""

assertEquals(3, parseForm(xml).getOrNull()?.fields?.size)
}

@Test
fun `fail with name of mixin that contains duplicates`() {
// language=XML
val xml =
"""
<mixin>
<form>
<input name="intro" type="TextLine"/>
<mixin name="bb"/>
</form>
</mixin>
"""

// language=XML
val xml2 =
"""
<mixin>
<form>
<input name="intro" type="TextArea"/>
</form>
</mixin>
"""

val mixinDependencies =
mapOf("aa" to xml, "bb" to xml2)
.map { (name, xml) -> parseMixinDependencyModel(stringToXMLDocument(xml).getChildNodeAtXPath("mixin/form")!!, name) }

val exception = assertFailsWith<DuplicateFieldNameException> { parseMixin(mixinDependencies.first(), mixinDependencies) }

assertEquals(
"Duplicate field name \"intro\" in \"site/mixins/aa/aa.xml\". A field name can only be used once in the same object.",
exception.message,
)
}
}
Loading