Skip to content

Commit

Permalink
feat(connector-corda): add JSON classname->JVM class object deserialize
Browse files Browse the repository at this point in the history
1. This allows the API clients to specify a class name from which the backend
will retrieve the JVM Class<?> object.
2. It is very simple under the hood it just uses `Class.forName(x)`
3. It is needed to be able to do this because when passing in flow parameters
sometimes the arguments are Class<?> objects and so this was a feature gap.

Signed-off-by: Peter Somogyvari <[email protected]>
  • Loading branch information
petermetz committed Jun 27, 2024
1 parent 2312612 commit 0508f14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class JsonJvmObjectDeserializer(
val constructorArgs: Array<Any?> = jvmObject.jvmCtorArgs.map { x -> instantiate(x) }.toTypedArray()

when {
Class::class.java.isAssignableFrom(clazz) -> {
val x = constructorArgs.map { ca -> ca as String }.first()
return Class.forName(x)
}
DoubleArray::class.java.isAssignableFrom(clazz) -> {
return constructorArgs
.map { ca -> ca as Double }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,35 @@ enum class Direction {
NORTH, SOUTH, WEST, EAST
}

class TestTxData {}
class JsonJvmObjectDeserializerTest {

companion object {
val deserializer = JsonJvmObjectDeserializer()
}


@Test
fun classForNameHappyPath() {

val jvmObject = JvmObject(
jvmTypeKind = JvmTypeKind.REFERENCE,
jvmType = JvmType(
fqClassName = Class::class.java.name
),
jvmCtorArgs = listOf(
JvmObject(
jvmTypeKind = JvmTypeKind.PRIMITIVE,
jvmType = JvmType(String::class.java.name),
primitiveValue = TestTxData::class.java.name
)
)
)

val deserializedObject = deserializer.instantiate(jvmObject)

assert(deserializedObject == TestTxData::class.java)
}
@Test
fun enumHappyPath() {
val actual = Direction.WEST
Expand Down

0 comments on commit 0508f14

Please sign in to comment.