From 5b643ab40f8d0618cec1b95fcb978272ae9b7cc0 Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:17:40 +0000 Subject: [PATCH] issues/258 - Fix for option with byte array child. --- build.gradle | 2 +- .../clvalue/AbstractCLValueWithChildren.java | 6 +-- .../cltype/AbstractCLTypeWithChildren.java | 4 ++ .../model/clvalue/cltype/CLTypeOption.java | 3 +- .../sdk/model/clvalue/CLValueTests.java | 49 +++++++++++++++++++ 5 files changed, 59 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index b1670ac4b..753ab0d10 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ apply plugin: 'java' group = 'network.casper' // Version number update for release -version='2.5.2' +version='2.5.3' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java b/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java index 82cac2402..a21f1e989 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java +++ b/src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java @@ -38,10 +38,10 @@ protected void childTypesSet() { protected void populateChildTypesFromParent(final AbstractCLValue child, final AbstractCLType type) { if (type instanceof AbstractCLTypeWithChildren) { if (child.getClType() instanceof AbstractCLTypeWithChildren) { - ((AbstractCLTypeWithChildren) child.getClType()).setChildTypes(((AbstractCLTypeWithChildren)type).getChildTypes()); - } else if (child instanceof CLValueByteArray) { - ((CLValueByteArray) child).setClType((CLTypeByteArray) ((AbstractCLTypeWithChildren)type).getChildTypes().get(0)); + ((AbstractCLTypeWithChildren) child.getClType()).setChildTypes(((AbstractCLTypeWithChildren) type).getChildTypes()); } + } else if (type instanceof CLTypeByteArray) { + ((CLValueByteArray) child).setClType((CLTypeByteArray) type); } } diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java index 3729412b8..8a1cc8842 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/AbstractCLTypeWithChildren.java @@ -40,6 +40,10 @@ protected void setChildTypeObjects(final List childTypeObjects) this.loadCLTypes(childTypeObjects); } + public List getChildTypes() { + return childTypes == null ? this.childTypes = new ArrayList<>() : childTypes; + } + protected List getChildTypeObjects() { if (this.childTypeObjects == null) { this.childTypeObjects = new ArrayList<>(); diff --git a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java index 9c9744264..3736633ba 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java +++ b/src/main/java/com/casper/sdk/model/clvalue/cltype/CLTypeOption.java @@ -69,7 +69,8 @@ public void serializeChildTypes(final SerializerBuffer ser) throws NoSuchTypeExc } @Override - public void deserializeChildTypes(final DeserializerBuffer deser) throws ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { + public void deserializeChildTypes(final DeserializerBuffer deser) throws + ValueDeserializationException, NoSuchTypeException, DynamicInstanceException { setOptionType(deserializeChildType(deser)); } } diff --git a/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java b/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java index ea198457f..8f6662a3e 100644 --- a/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java +++ b/src/test/java/com/casper/sdk/model/clvalue/CLValueTests.java @@ -7,6 +7,7 @@ import com.casper.sdk.model.clvalue.serde.Target; import com.casper.sdk.model.deploy.NamedArg; import com.casper.sdk.model.key.PublicKey; +import com.fasterxml.jackson.databind.ObjectMapper; import com.syntifi.crypto.key.AbstractPublicKey; import com.syntifi.crypto.key.Ed25519PrivateKey; import com.syntifi.crypto.key.Secp256k1PrivateKey; @@ -384,4 +385,52 @@ void nestedOptionWithMap() throws Exception { final CLValueOption deserialized = (CLValueOption) clValueOption.deserialize(new DeserializerBuffer(bytes), Target.BYTE); assertThat(deserialized.getBytes(), is(clValueOption.getBytes())); } + + @Test + void testOptionByteTypeSerialization() throws Exception { + + final String hexBytes = "d2029649"; + CLValueOption clValueOption = new CLValueOption(Optional.of(new CLValueByteArray(Hex.decode(hexBytes)))); + final SerializerBuffer ser = new SerializerBuffer(); + assertThat(clValueOption.getBytes(), is("01" + hexBytes)); + clValueOption.serialize(ser, Target.BYTE); + + final byte[] bytes = ser.toByteArray(); + + assertThat(bytes, is(Hex.decode("0500000001d20296490D0F04000000"))); + + final String json = "{\n" + + " \"cl_type\" : {\n" + + " \"Option\" : {\n" + + " \"ByteArray\" : 4\n" + + " }\n" + + " },\n" + + " \"bytes\" : \"01d2029649\",\n" + + " \"parsed\" : \"d2029649\"\n" + + "}"; + + final ObjectMapper objectMapper = new ObjectMapper(); + final CLValueOption fromJson = objectMapper.reader().readValue(json, CLValueOption.class); + assertThat(fromJson.getBytes(), is("01" + hexBytes)); + } + + @Test + void testEmptyOptionSerialization() throws Exception { + + CLValueOption clValueOption = new CLValueOption(Optional.empty()); + clValueOption.getClType().setOptionType(new CLTypeString()); + final SerializerBuffer ser = new SerializerBuffer(); + assertThat(clValueOption.getBytes(), is("00")); + clValueOption.serialize(ser, Target.BYTE); + + final byte[] bytes = ser.toByteArray(); + assertThat(bytes, is(Hex.decode("01000000000D0A"))); + + final CLValueOption deserialized = (CLValueOption) clValueOption.deserialize(new DeserializerBuffer(bytes), Target.BYTE); + assertThat(deserialized.getBytes(), is(clValueOption.getBytes())); + assertThat(deserialized.getClType().getOptionType().getClTypeData(), is(CLTypeData.STRING)); + + clValueOption = new CLValueOption(Optional.empty()); + assertThat(clValueOption.getBytes(), is("00")); + } }