From 029a18e96b06365ea828b4152dd9829169cff313 Mon Sep 17 00:00:00 2001 From: meywood <105049338+meywood@users.noreply.github.com> Date: Wed, 6 Mar 2024 18:30:23 +0000 Subject: [PATCH] issues/249 - Fix for nested option types that contain children --- .../sdk/model/clvalue/CLValueOption.java | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java b/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java index 8d22607dc..093ef84c2 100644 --- a/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java +++ b/src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java @@ -1,11 +1,7 @@ package com.casper.sdk.model.clvalue; import com.casper.sdk.exception.NoSuchTypeException; -import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren; -import com.casper.sdk.model.clvalue.cltype.CLTypeData; -import com.casper.sdk.model.clvalue.cltype.CLTypeList; -import com.casper.sdk.model.clvalue.cltype.CLTypeOption; -import com.casper.sdk.model.clvalue.serde.Target; +import com.casper.sdk.model.clvalue.cltype.*; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSetter; import dev.oak3.sbs4j.DeserializerBuffer; @@ -41,24 +37,12 @@ public void setClType(final CLTypeOption clType) { childTypesSet(); } + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public CLValueOption(final Optional> value) throws ValueSerializationException { setChildTypes(value); this.setValue(value); } - @Override - public void serialize(final SerializerBuffer ser, final Target target) throws ValueSerializationException, NoSuchTypeException { - - super.serialize(ser, target); - - if (target.equals(Target.BYTE)) { - Optional> child = getValue(); - if (child.isPresent()) { - child.get().encodeType(ser); - } - } - } - @Override protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException { @@ -72,7 +56,7 @@ protected void serializeValue(final SerializerBuffer ser) throws ValueSerializat ((AbstractCLTypeWithChildren) value.get().getClType()) .setChildTypes(((AbstractCLTypeWithChildren) clType.getOptionType()).getChildTypes()); } - if (value.isPresent() && isPresent.getValue().equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(isPresent.getValue()) && value.isPresent()) { value.get().serialize(serVal); } @@ -96,15 +80,28 @@ public void deserializeCustom(final DeserializerBuffer deser) throws Exception { } else if (child.getClType() instanceof AbstractCLTypeWithChildren) { ((AbstractCLTypeWithChildren) child.getClType()) .setChildTypes(((AbstractCLTypeWithChildren) clType.getOptionType()).getChildTypes()); + } else if (child instanceof CLValueByteArray) { + // Byte arrays require their length to be set to correctly deserialize + ((CLValueByteArray) child).setClType((CLTypeByteArray) clType.getOptionType()); } - if (isPresent.getValue().equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(isPresent.getValue())) { child.deserializeCustom(deser); } setValue(Optional.of(child)); } + @Override + protected void encodeType(final SerializerBuffer ser) throws NoSuchTypeException { + super.encodeType(ser); + + Optional> child = getValue(); + if (child.isPresent()) { + child.get().encodeType(ser); + } + } + @Override protected void setChildTypes(final Optional> value) { if (value.isPresent()) {