From 0508f14c64e452f8f89881db363cd00b3c8c255c Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Fri, 21 Jun 2024 18:21:31 +0000 Subject: [PATCH] feat(connector-corda): add JSON classname->JVM class object deserialize 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 --- .../server/impl/JsonJvmObjectDeserializer.kt | 4 ++++ .../impl/JsonJvmObjectDeserializerTest.kt | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/main/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializer.kt b/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/main/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializer.kt index e4a20462aa..82c84b65a9 100644 --- a/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/main/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializer.kt +++ b/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/main/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializer.kt @@ -74,6 +74,10 @@ class JsonJvmObjectDeserializer( val constructorArgs: Array = 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 } diff --git a/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/test/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializerTest.kt b/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/test/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializerTest.kt index 0853fa7541..e9cc8f7c0c 100644 --- a/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/test/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializerTest.kt +++ b/packages/cactus-plugin-ledger-connector-corda/src/main-server/kotlin/gen/kotlin-spring/src/test/kotlin/org/hyperledger/cactus/plugin/ledger/connector/corda/server/impl/JsonJvmObjectDeserializerTest.kt @@ -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