CORDA-1229 - Fix issue with setter-based serialisation (#3051)

This commit is contained in:
Katelyn Baker 2018-05-01 20:52:39 +01:00 committed by GitHub
parent ee8d8a7618
commit 7701dad80b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 7 deletions

View File

@ -1,8 +1,14 @@
Changelog
=========
Here are brief summaries of what's changed between each snapshot release. This includes guidance on how to upgrade code
from the previous milestone release.
Here's a summary of what's changed in each Corda release. For guidance on how to upgrade code from the previous
release, see :doc:`upgrade-notes`.
Unreleased
==========
* Fix CORDA-1229. Setter-based serialization was broken with generic types when the property was stored
as the raw type, List for example.
.. _changelog_v3.1:

View File

@ -296,7 +296,7 @@ fun propertiesForSerializationFromSetters(
"takes too many arguments")
}
val setterType = setter.parameterTypes[0]!!
val setterType = setter.genericParameterTypes[0]!!
if ((property.value.field != null) &&
(!(TypeToken.of(property.value.field?.genericType!!).isSupertypeOf(setterType)))) {
@ -305,11 +305,11 @@ fun propertiesForSerializationFromSetters(
"${property.value.field?.genericType!!}")
}
// make sure the setter returns the same type (within inheritance bounds) the getter accepts
if (!(TypeToken.of (setterType).isSupertypeOf(getter.returnType))) {
// Make sure the getter returns the same type (within inheritance bounds) the setter accepts.
if (!(TypeToken.of (getter.genericReturnType).isSupertypeOf(setterType))) {
throw NotSerializableException("Defined setter for parameter ${property.value.field?.name} " +
"takes parameter of type $setterType yet the defined getter returns a value of type " +
"${getter.returnType}")
"${getter.returnType} [${getter.genericReturnType}]")
}
this += PropertyAccessorGetterSetter(
idx++,

View File

@ -7,6 +7,8 @@ import org.junit.Test;
import static org.junit.Assert.*;
import java.io.NotSerializableException;
import java.util.ArrayList;
import java.util.List;
public class SetterConstructorTests {
@ -64,6 +66,13 @@ public class SetterConstructorTests {
public void setC(int c) { this.c = c; }
}
static class CIntList {
private List<Integer> l;
public List getL() { return l; }
public void setL(List<Integer> l) { this.l = l; }
}
static class Inner1 {
private String a;
@ -305,4 +314,27 @@ public class SetterConstructorTests {
Assertions.assertThatThrownBy(() -> new SerializationOutput(factory1).serialize(tm)).isInstanceOf(
NotSerializableException.class);
}
// This not blowing up means it's working
@Test
public void intList() throws NotSerializableException {
CIntList cil = new CIntList();
List<Integer> l = new ArrayList<>();
l.add(1);
l.add(2);
l.add(3);
cil.setL(l);
EvolutionSerializerGetterBase evolutionSerializerGetter = new EvolutionSerializerGetter();
SerializerFactory factory1 = new SerializerFactory(
AllWhitelist.INSTANCE,
ClassLoader.getSystemClassLoader(),
evolutionSerializerGetter);
// if we've got super / sub types on the setter vs the underlying type the wrong way around this will
// explode. See CORDA-1229 (https://r3-cev.atlassian.net/browse/CORDA-1229)
new DeserializationInput(factory1).deserialize(new SerializationOutput(factory1).serialize(cil), CIntList.class);
}
}