Skip to content

Commit b1f5eb6

Browse files
abstraktorslisson
authored andcommitted
fix(mps-model-adapters): avoid PersistenceFacade$IncorrectModelReferenceFormatException
- using an empty string as model name caused the exception - now "unknown" is used instead of ""
1 parent bc71156 commit b1f5eb6

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.modelix.model.mpsadapters
2+
3+
import jetbrains.mps.smodel.SModelId
4+
import jetbrains.mps.smodel.SModelReference
5+
import org.jetbrains.mps.openapi.persistence.PersistenceFacade
6+
import java.util.UUID
7+
import kotlin.test.assertFailsWith
8+
9+
class ModelReferenceParsingTest : MpsAdaptersTestBase("SimpleProject") {
10+
11+
fun `test model reference can be serialized and deserialized`() {
12+
val originalReference = SModelReference(null, SModelId.regular(UUID.fromString("9858ad24-9b93-4fb4-b55d-1d5b38a5d03b")), "my.model")
13+
val referenceWithoutName = SModelReference(null, SModelId.regular(UUID.fromString("9858ad24-9b93-4fb4-b55d-1d5b38a5d03b")), "")
14+
assertEquals("r:9858ad24-9b93-4fb4-b55d-1d5b38a5d03b()", referenceWithoutName.toString())
15+
16+
// MPS fails to parse a reference that it serialized itself.
17+
// This can be considered an MPS bug, but we have to compensate for that.
18+
// This check ensures that we found the correct cause for this exception.
19+
assertFailsWith(PersistenceFacade.IncorrectModelReferenceFormatException::class) {
20+
PersistenceFacade.getInstance().createModelReference(referenceWithoutName.toString())
21+
}
22+
23+
// Modelix is expected to remove the name, because MPS ignores the name when comparing two model references.
24+
// By not storing the name in Modelix we avoid any inconsistent behavior after a model renaming.
25+
assertEquals("mps-model:r:9858ad24-9b93-4fb4-b55d-1d5b38a5d03b", originalReference.toModelix().serialize())
26+
27+
// Ensure a reference is still parsable by MPS after it was converted to and from Modelix.
28+
assertEquals(
29+
"r:9858ad24-9b93-4fb4-b55d-1d5b38a5d03b(unknown)",
30+
PersistenceFacade.getInstance().createModelReference(originalReference.toModelix().toMPS().toString()).toString(),
31+
)
32+
}
33+
}

mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/MPSReferences.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.modelix.model.mpsadapters
22

33
import jetbrains.mps.smodel.SNodePointer
44
import jetbrains.mps.util.StringUtil
5+
import org.jetbrains.mps.openapi.model.SModelName
56
import org.jetbrains.mps.openapi.model.SModelReference
67
import org.jetbrains.mps.openapi.model.SNodeReference
78
import org.jetbrains.mps.openapi.module.SModuleId
@@ -23,7 +24,7 @@ fun SModelReference.toModelix() = MPSModelReference(moduleReference?.toModelix()
2324
fun MPSModelReference.toMPS(): SModelReference = PersistenceFacade.getInstance().createModelReference(
2425
moduleReference?.toMPS(),
2526
PersistenceFacade.getInstance().createModelId(modelId),
26-
"",
27+
SModelName("unknown"), // we need a non-empty model name here to avoid PersistenceFacade$IncorrectModelReferenceFormatException: Incomplete model reference, the presentation part is absent
2728
)
2829

2930
fun SNodeReference.toModelix() = MPSNodeReference(

0 commit comments

Comments
 (0)