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:
Dominic Fox 2018-12-04 11:27:43 +00:00 committed by GitHub
parent c2986ca31d
commit 36c5b0cbae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -149,14 +149,18 @@ class DefaultEvolutionSerializerFactory(
val conversions = members.associate { it to findLocal(it) } val conversions = members.associate { it to findLocal(it) }
val convertedOrdinals = remoteOrdinals.asSequence().map { (member, ord) -> ord to conversions[member]!! }.toMap() val convertedOrdinals = remoteOrdinals.asSequence().map { (member, ord) -> ord to conversions[member]!! }.toMap()
if (localOrdinals.any { (name, ordinal) -> convertedOrdinals[ordinal] != name })
throw EvolutionSerializationException( if (constantsAreReordered(localOrdinals, convertedOrdinals)) throw EvolutionSerializationException(this,
this,
"Constants have been reordered, additions must be appended to the end") "Constants have been reordered, additions must be appended to the end")
return EnumEvolutionSerializer(localTypeInformation.observedType, localSerializerFactory, conversions, localOrdinals) 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( private fun RemoteTypeInformation.Composable.buildComposableEvolutionSerializer(
localTypeInformation: LocalTypeInformation.Composable, localTypeInformation: LocalTypeInformation.Composable,
constructor: LocalConstructorInformation, constructor: LocalConstructorInformation,

View File

@ -13,6 +13,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.Ignore import org.junit.Ignore
import org.junit.Test import org.junit.Test
import java.io.File
import java.io.NotSerializableException import java.io.NotSerializableException
import java.net.URI import java.net.URI
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -424,4 +425,33 @@ class EnumEvolveTests {
EvolvabilityTests::class.java.getResource(resource).readBytes())) EvolvabilityTests::class.java.getResource(resource).readBytes()))
}.isInstanceOf(NotSerializableException::class.java) }.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)
}
} }