Skip to content

Commit

Permalink
Merge pull request #322 from modelix/feature/MODELIX-610_configurable…
Browse files Browse the repository at this point in the history
…-alias-key

fix(model-api-gen): disambiguate meta properties and metamodel members
  • Loading branch information
languitar authored Nov 14, 2023
2 parents e788d3c + 1e49079 commit 31a6720
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ internal class ProcessedLanguageSet(dataList: List<LanguageData>) : IProcessedLa
private fun process() {
initIndexes()
resolveConceptReferences()
fixRoleConflicts()
collectConceptMetaProperties()
fixRoleConflicts()
}

private fun collectConceptMetaProperties() {
Expand Down Expand Up @@ -128,8 +128,9 @@ internal class ProcessedLanguageSet(dataList: List<LanguageData>) : IProcessedLa
sameInHierarchyConflicts.forEach { it.generatedName += "_" + it.concept.name }

// replace illegal names
val illegalNames = reservedPropertyNames + conceptMetaProperties
allConcepts.flatMap { it.getOwnRoles() }.forEach {
if (reservedPropertyNames.contains(it.generatedName)) {
if (illegalNames.contains(it.generatedName)) {
it.generatedName += getTypeSuffix(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,35 @@ class KotlinGeneratorTest {
outputDir.deleteRecursively()
}
}

@OptIn(ExperimentalPathApi::class)
@Test
fun `avoids name clashes for concept meta properties`() {
val input = """
name: org.modelix.entities
concepts:
- name: Entity
properties:
- name: alias
children: []
metaProperties:
alias: this should conflict with the property of the same name
enums: []
""".trimIndent()

val language = Yaml.default.decodeFromString<LanguageData>(input)
val outputDir = createTempDirectory()
try {
MetaModelGenerator(outputDir).generate(LanguageSet(listOf(language)).process())

val fileContents = outputDir.resolve("org/modelix/entities/Entity.kt").readText()
assertContains(
fileContents,
"alias_property",
message = "The alias property must have been disambiguated by appending the type to the name.",
)
} finally {
outputDir.deleteRecursively()
}
}
}

0 comments on commit 31a6720

Please sign in to comment.