mirror of
https://github.com/corda/corda.git
synced 2024-12-20 05:28:21 +00:00
CORDA-3390: Allow EvolutionSerializer to handle primitive types becoming nullable. (#5661)
This commit is contained in:
parent
43856319bd
commit
b2c944bb0e
@ -39,7 +39,8 @@ dependencies {
|
||||
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:${junit_vintage_version}"
|
||||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junit_jupiter_version}"
|
||||
testRuntimeOnly "org.junit.platform:junit-platform-launcher:${junit_platform_version}"
|
||||
|
||||
testRuntimeOnly "org.slf4j:slf4j-simple:$slf4j_version"
|
||||
|
||||
testCompile "org.assertj:assertj-core:$assertj_version"
|
||||
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
|
||||
testCompile "org.mockito:mockito-core:$mockito_version"
|
||||
|
@ -105,7 +105,13 @@ class DefaultEvolutionSerializerFactory(
|
||||
evolverProperties.all { (name, evolverProperty) ->
|
||||
val propertyType = propertyTypes[name]
|
||||
if (propertyType == null) !evolverProperty.isMandatory
|
||||
else evolverProperty.type.observedType.asClass().isAssignableFrom(propertyType)
|
||||
else {
|
||||
// Check that we can assign the remote property value to its local equivalent.
|
||||
// This includes assigning a primitive type to its equivalent "boxed" type.
|
||||
val evolverPropertyType = evolverProperty.type.observedType.asClass()
|
||||
evolverPropertyType.isAssignableFrom(propertyType)
|
||||
|| (propertyType.isPrimitive && evolverPropertyType.isAssignableFrom(propertyType.kotlin.javaObjectType))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import kotlin.test.assertEquals
|
||||
import net.corda.serialization.internal.amqp.custom.InstantSerializer
|
||||
import net.corda.serialization.internal.amqp.testutils.ProjectStructure.projectRootDir
|
||||
import java.math.BigInteger
|
||||
import kotlin.test.fail
|
||||
|
||||
// To regenerate any of the binary test files do the following
|
||||
//
|
||||
@ -36,7 +37,7 @@ class EvolvabilityTests {
|
||||
// When regenerating the test files this needs to be set to the file system location of the resource files
|
||||
@Suppress("UNUSED")
|
||||
var localPath: URI = projectRootDir.toUri().resolve(
|
||||
"serialization-test/src/test/resources/net/corda/serialization/internal/amqp")
|
||||
"serialization/src/test/resources/net/corda/serialization/internal/amqp")
|
||||
|
||||
companion object {
|
||||
private val DUMMY_NOTARY_NAME = CordaX500Name("Notary Service", "Zurich", "CH")
|
||||
@ -744,4 +745,27 @@ class EvolvabilityTests {
|
||||
|
||||
assertEquals(10, deserialized.parameterized?.a)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun addMandatoryFieldWithAltConstructorAndMakeExistingIntFieldNullable() {
|
||||
val sf = testDefaultFactory()
|
||||
val resource = "EvolvabilityTests.addMandatoryFieldWithAltConstructorAndMakeExistingIntFieldNullable"
|
||||
|
||||
// Original version of the class as it was serialised
|
||||
// data class CC(val a: Int)
|
||||
// File(URI("$localPath/$resource")).writeBytes(SerializationOutput(sf).serialize(CC(1)).bytes)
|
||||
|
||||
data class CC(val a: Int?, val b: Int) {
|
||||
@DeprecatedConstructorForDeserialization(1)
|
||||
@Suppress("unused")
|
||||
constructor(a: Int) : this(a, 42)
|
||||
}
|
||||
|
||||
val url = EvolvabilityTests::class.java.getResource(resource) ?: fail("Not found!")
|
||||
val sc2 = url.readBytes()
|
||||
val deserializedCC = DeserializationInput(sf).deserialize(SerializedBytes<CC>(sc2))
|
||||
|
||||
assertEquals(1, deserializedCC.a)
|
||||
assertEquals(42, deserializedCC.b)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user