mirror of
https://github.com/corda/corda.git
synced 2024-12-20 05:28:21 +00:00
CORDA-2264 enable appending enum constants (#4347)
* CORDA-2264 enable appending enum constants * Remove paths B and C, rename test
This commit is contained in:
parent
c2986ca31d
commit
36c5b0cbae
@ -149,14 +149,18 @@ class DefaultEvolutionSerializerFactory(
|
||||
|
||||
val conversions = members.associate { it to findLocal(it) }
|
||||
val convertedOrdinals = remoteOrdinals.asSequence().map { (member, ord) -> ord to conversions[member]!! }.toMap()
|
||||
if (localOrdinals.any { (name, ordinal) -> convertedOrdinals[ordinal] != name })
|
||||
throw EvolutionSerializationException(
|
||||
this,
|
||||
"Constants have been reordered, additions must be appended to the end")
|
||||
|
||||
if (constantsAreReordered(localOrdinals, convertedOrdinals)) throw EvolutionSerializationException(this,
|
||||
"Constants have been reordered, additions must be appended to the end")
|
||||
|
||||
return EnumEvolutionSerializer(localTypeInformation.observedType, localSerializerFactory, conversions, localOrdinals)
|
||||
}
|
||||
|
||||
private fun constantsAreReordered(localOrdinals: Map<String, Int>, convertedOrdinals: Map<Int, String>): Boolean =
|
||||
if (localOrdinals.size <= convertedOrdinals.size) {
|
||||
localOrdinals.any { (name, ordinal) -> convertedOrdinals[ordinal] != name }
|
||||
} else convertedOrdinals.any { (ordinal, name) -> localOrdinals[name] != ordinal }
|
||||
|
||||
private fun RemoteTypeInformation.Composable.buildComposableEvolutionSerializer(
|
||||
localTypeInformation: LocalTypeInformation.Composable,
|
||||
constructor: LocalConstructorInformation,
|
||||
|
@ -13,6 +13,7 @@ import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.junit.Ignore
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
import java.io.NotSerializableException
|
||||
import java.net.URI
|
||||
import kotlin.test.assertEquals
|
||||
@ -424,4 +425,33 @@ class EnumEvolveTests {
|
||||
EvolvabilityTests::class.java.getResource(resource).readBytes()))
|
||||
}.isInstanceOf(NotSerializableException::class.java)
|
||||
}
|
||||
|
||||
// Version of the class as it was serialised
|
||||
//
|
||||
// enum class ExtendedEnum { A, B, C }
|
||||
//
|
||||
// Version of the class as it's used in the test
|
||||
@CordaSerializationTransformEnumDefaults (
|
||||
CordaSerializationTransformEnumDefault("E", "C"),
|
||||
CordaSerializationTransformEnumDefault("D", "C")
|
||||
)
|
||||
enum class ExtendedEnum { A, B, C, D, E}
|
||||
|
||||
// See https://r3-cev.atlassian.net/browse/CORDA-2264.
|
||||
@Test
|
||||
fun extendEnum() {
|
||||
val resource = "${javaClass.simpleName}.${testName()}"
|
||||
val sf = testDefaultFactory()
|
||||
|
||||
data class C(val e: ExtendedEnum)
|
||||
|
||||
// Uncomment to re-generate test files
|
||||
// val so = SerializationOutput(sf)
|
||||
// File(URI("$localPath/$resource.A")).writeBytes(so.serialize(C(ExtendedEnum.A)).bytes)
|
||||
|
||||
val path1 = EvolvabilityTests::class.java.getResource("$resource.A")
|
||||
|
||||
val obj1 = DeserializationInput(sf).deserialize(SerializedBytes<C>(File(path1.toURI()).readBytes()))
|
||||
assertEquals(ExtendedEnum.A, obj1.e)
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user