Skip to content

Commit 1e49079

Browse files
committed
fix(model-api-gen): disambiguate meta properties and metamodel members
Avoids compilation errors when the metaProperties contain something that also exists in the metamodel by treating the metaProperties keys as reserved names. Fixes: MODELIX-610
1 parent e788d3c commit 1e49079

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

model-api-gen/src/main/kotlin/org/modelix/metamodel/generator/GeneratorInput.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ internal class ProcessedLanguageSet(dataList: List<LanguageData>) : IProcessedLa
6262
private fun process() {
6363
initIndexes()
6464
resolveConceptReferences()
65-
fixRoleConflicts()
6665
collectConceptMetaProperties()
66+
fixRoleConflicts()
6767
}
6868

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

130130
// replace illegal names
131+
val illegalNames = reservedPropertyNames + conceptMetaProperties
131132
allConcepts.flatMap { it.getOwnRoles() }.forEach {
132-
if (reservedPropertyNames.contains(it.generatedName)) {
133+
if (illegalNames.contains(it.generatedName)) {
133134
it.generatedName += getTypeSuffix(it)
134135
}
135136
}

model-api-gen/src/test/kotlin/org/modelix/metamodel/generator/KotlinGeneratorTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,35 @@ class KotlinGeneratorTest {
124124
outputDir.deleteRecursively()
125125
}
126126
}
127+
128+
@OptIn(ExperimentalPathApi::class)
129+
@Test
130+
fun `avoids name clashes for concept meta properties`() {
131+
val input = """
132+
name: org.modelix.entities
133+
concepts:
134+
- name: Entity
135+
properties:
136+
- name: alias
137+
children: []
138+
metaProperties:
139+
alias: this should conflict with the property of the same name
140+
enums: []
141+
""".trimIndent()
142+
143+
val language = Yaml.default.decodeFromString<LanguageData>(input)
144+
val outputDir = createTempDirectory()
145+
try {
146+
MetaModelGenerator(outputDir).generate(LanguageSet(listOf(language)).process())
147+
148+
val fileContents = outputDir.resolve("org/modelix/entities/Entity.kt").readText()
149+
assertContains(
150+
fileContents,
151+
"alias_property",
152+
message = "The alias property must have been disambiguated by appending the type to the name.",
153+
)
154+
} finally {
155+
outputDir.deleteRecursively()
156+
}
157+
}
127158
}

0 commit comments

Comments
 (0)