mirror of
https://github.com/corda/corda.git
synced 2025-02-21 09:51:57 +00:00
CORDA-1825 user-friendly error when serializing non-composable types (#4290)
* CORDA-1825 user-friendly error when trying to serialize non-composable type * Tests for @CordaSerializable
This commit is contained in:
parent
4ebca4846c
commit
81905c67ea
@ -14,6 +14,11 @@ interface ObjectSerializer : AMQPSerializer<Any> {
|
||||
|
||||
companion object {
|
||||
fun make(typeInformation: LocalTypeInformation, factory: LocalSerializerFactory): ObjectSerializer {
|
||||
if (typeInformation is LocalTypeInformation.NonComposable)
|
||||
throw NotSerializableException(
|
||||
"Trying to build an object serializer for ${typeInformation.typeIdentifier.prettyPrint(false)}, " +
|
||||
"but it is not constructible from its public properties, and so requires a custom serialiser.")
|
||||
|
||||
val typeDescriptor = factory.createDescriptor(typeInformation)
|
||||
val typeNotation = TypeNotationGenerator.getTypeNotation(typeInformation, typeDescriptor)
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
package net.corda.serialization.internal.amqp
|
||||
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import net.corda.serialization.internal.amqp.testutils.*
|
||||
import org.junit.Test
|
||||
import java.io.NotSerializableException
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.fail
|
||||
|
||||
// Prior to certain fixes being made within the [PropertySerializaer] classes these simple
|
||||
// deserialization operations would've blown up with type mismatch errors where the deserlized
|
||||
@ -527,5 +532,65 @@ class DeserializeSimpleTypesTests {
|
||||
DeserializationInput(sf2).deserialize(serializedA.obj)
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
class Garbo private constructor(value: Int) {
|
||||
companion object {
|
||||
fun make(value: Int) = Garbo(value)
|
||||
}
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
class Greta(val garbo: Garbo)
|
||||
|
||||
@CordaSerializable
|
||||
class Owner(val value: PropertyWithoutCordaSerializable)
|
||||
|
||||
class PropertyWithoutCordaSerializable(val value: Int)
|
||||
|
||||
@Test
|
||||
fun classHasNoPublicConstructor() {
|
||||
assertFailsWithMessage("Trying to build an object serializer for ${Garbo::class.java.name}, " +
|
||||
"but it is not constructible from its public properties, and so requires a custom serialiser.") {
|
||||
TestSerializationOutput(VERBOSE, sf1).serializeAndReturnSchema(Garbo.make(1))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun propertyClassHasNoPublicConstructor() {
|
||||
assertFailsWithMessage("Trying to build an object serializer for ${Greta::class.java.name}, " +
|
||||
"but it is not constructible from its public properties, and so requires a custom serialiser.") {
|
||||
TestSerializationOutput(VERBOSE, sf1).serializeAndReturnSchema(Greta(Garbo.make(1)))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun notWhitelistedError() {
|
||||
val factory = testDefaultFactoryWithWhitelist()
|
||||
assertFailsWithMessage(
|
||||
"Class \"class ${PropertyWithoutCordaSerializable::class.java.name}\" " +
|
||||
"is not on the whitelist or annotated with @CordaSerializable.") {
|
||||
TestSerializationOutput(VERBOSE, factory).serialize(PropertyWithoutCordaSerializable(1))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun propertyClassNotWhitelistedError() {
|
||||
val factory = testDefaultFactoryWithWhitelist()
|
||||
assertFailsWithMessage(
|
||||
"Class \"class ${PropertyWithoutCordaSerializable::class.java.name}\" " +
|
||||
"is not on the whitelist or annotated with @CordaSerializable.") {
|
||||
TestSerializationOutput(VERBOSE, factory).serialize(Owner(PropertyWithoutCordaSerializable(1)))
|
||||
}
|
||||
}
|
||||
|
||||
private fun assertFailsWithMessage(expectedMessage: String, block: () -> Unit) {
|
||||
try {
|
||||
block()
|
||||
fail("Expected an exception, but none was thrown")
|
||||
} catch (e: Exception) {
|
||||
assertEquals(expectedMessage, e.message)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user