Skip to content

Commit b848eda

Browse files
authored
Merge pull request #275 from /issues/274
Issues/274 - Refactor JSON deserialization to use @JsonCreator to ensure that cl_type is parsed prior to setting any value fields in AbstractCLValueWithChildren and CLValueByteArray classes.
2 parents 67be93d + 1cfee7e commit b848eda

11 files changed

Lines changed: 264 additions & 23 deletions

File tree

src/main/java/com/casper/sdk/model/clvalue/AbstractCLValueWithChildren.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
@EqualsAndHashCode(callSuper = true)
2222
public abstract class AbstractCLValueWithChildren<T, P extends AbstractCLTypeWithChildren> extends AbstractCLValue<T, P> {
23+
2324
protected abstract void setChildTypes(T value);
2425

2526
/**
@@ -30,7 +31,7 @@ public abstract class AbstractCLValueWithChildren<T, P extends AbstractCLTypeWit
3031
*/
3132
@SneakyThrows({ValueDeserializationException.class})
3233
protected void childTypesSet() {
33-
if (!getBytes().isEmpty()) {
34+
if (!getBytes().isEmpty() && getClType().isDeserializable()) {
3435
this.deserialize(new DeserializerBuffer(this.getBytes()));
3536
}
3637
}

src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeAny;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
56
import com.fasterxml.jackson.annotation.JsonGetter;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
68
import com.fasterxml.jackson.annotation.JsonSetter;
79
import dev.oak3.sbs4j.DeserializerBuffer;
810
import dev.oak3.sbs4j.SerializerBuffer;
@@ -28,8 +30,19 @@
2830
@Setter
2931
@NoArgsConstructor
3032
public class CLValueAny extends AbstractCLValue<byte[], CLTypeAny> {
33+
3134
private CLTypeAny clType = new CLTypeAny();
3235

36+
@JsonCreator
37+
public CLValueAny(@JsonProperty("cl_type") final CLTypeAny clType,
38+
@JsonProperty("bytes") final String bytes,
39+
@JsonProperty("parsed") final Object parsed) throws ValueSerializationException {
40+
setBytes(bytes);
41+
setClType(clType);
42+
setValue(Hex.decode(bytes));
43+
setParsed(parsed);
44+
}
45+
3346
@JsonSetter("cl_type")
3447
@ExcludeFromJacocoGeneratedReport
3548
protected void setJsonClType(final CLTypeAny clType) {

src/main/java/com/casper/sdk/model/clvalue/CLValueByteArray.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeByteArray;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
56
import com.fasterxml.jackson.annotation.JsonProperty;
67
import dev.oak3.sbs4j.DeserializerBuffer;
78
import dev.oak3.sbs4j.SerializerBuffer;
@@ -27,9 +28,19 @@
2728
@Setter
2829
@NoArgsConstructor
2930
public class CLValueByteArray extends AbstractCLValue<byte[], CLTypeByteArray> {
31+
3032
@JsonProperty("cl_type")
3133
private CLTypeByteArray clType = new CLTypeByteArray();
3234

35+
@JsonCreator
36+
public CLValueByteArray(@JsonProperty("cl_type") final CLTypeByteArray clType,
37+
@JsonProperty("bytes") final String bytes,
38+
@JsonProperty("parsed") final String parsed) throws ValueSerializationException {
39+
this.clType = clType;
40+
setValue(Hex.decode(bytes));
41+
setParsed(parsed);
42+
}
43+
3344
public CLValueByteArray(final byte[] value) throws ValueSerializationException {
3445
this.setValue(value);
3546
this.clType.setLength(value.length);

src/main/java/com/casper/sdk/model/clvalue/CLValueList.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeList;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
56
import com.fasterxml.jackson.annotation.JsonProperty;
6-
import com.fasterxml.jackson.annotation.JsonSetter;
77
import dev.oak3.sbs4j.DeserializerBuffer;
88
import dev.oak3.sbs4j.SerializerBuffer;
99
import dev.oak3.sbs4j.exception.ValueSerializationException;
@@ -30,20 +30,29 @@
3030
@EqualsAndHashCode(callSuper = true)
3131
@NoArgsConstructor
3232
public class CLValueList extends AbstractCLValueWithChildren<List<? extends AbstractCLValue<?, ?>>, CLTypeList> {
33+
3334
@JsonProperty("cl_type")
3435
private CLTypeList clType = new CLTypeList();
3536

36-
@JsonSetter("cl_type")
37-
public void setClType(final CLTypeList clType) {
38-
this.clType = clType;
39-
childTypesSet();
37+
@JsonCreator
38+
public CLValueList(@JsonProperty("cl_type") final CLTypeList clType,
39+
@JsonProperty("bytes") final String bytes,
40+
@JsonProperty("parsed") final Object parsed) {
41+
setBytes(bytes);
42+
setClType(clType);
43+
setParsed(parsed);
4044
}
4145

4246
public CLValueList(final List<? extends AbstractCLValue<?, ?>> value) throws ValueSerializationException {
4347
setChildTypes(value);
4448
this.setValue(value);
4549
}
4650

51+
public void setClType(final CLTypeList clType) {
52+
this.clType = clType;
53+
childTypesSet();
54+
}
55+
4756
@Override
4857
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
4958

src/main/java/com/casper/sdk/model/clvalue/CLValueMap.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeMap;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
56
import com.fasterxml.jackson.annotation.JsonIgnore;
67
import com.fasterxml.jackson.annotation.JsonProperty;
7-
import com.fasterxml.jackson.annotation.JsonSetter;
88
import dev.oak3.sbs4j.DeserializerBuffer;
99
import dev.oak3.sbs4j.SerializerBuffer;
1010
import dev.oak3.sbs4j.exception.ValueSerializationException;
@@ -33,10 +33,19 @@
3333
@NoArgsConstructor
3434
public class CLValueMap extends
3535
AbstractCLValueWithChildren<Map<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeMap> {
36+
3637
@JsonProperty("cl_type")
3738
private CLTypeMap clType = new CLTypeMap();
3839

39-
@JsonSetter("cl_type")
40+
@JsonCreator
41+
public CLValueMap(@JsonProperty("cl_type") final CLTypeMap clType,
42+
@JsonProperty("bytes") final String bytes,
43+
@JsonProperty("parsed") final Object parsed) {
44+
setBytes(bytes);
45+
setClType(clType);
46+
setParsed(parsed);
47+
}
48+
4049
public void setClType(CLTypeMap clType) {
4150
this.clType = clType;
4251
childTypesSet();

src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
55
import com.casper.sdk.model.clvalue.cltype.CLTypeOption;
6+
import com.fasterxml.jackson.annotation.JsonCreator;
67
import com.fasterxml.jackson.annotation.JsonProperty;
7-
import com.fasterxml.jackson.annotation.JsonSetter;
88
import dev.oak3.sbs4j.DeserializerBuffer;
99
import dev.oak3.sbs4j.SerializerBuffer;
1010
import dev.oak3.sbs4j.exception.ValueSerializationException;
@@ -29,10 +29,19 @@
2929
@EqualsAndHashCode(callSuper = true)
3030
@NoArgsConstructor
3131
public class CLValueOption extends AbstractCLValueWithChildren<Optional<AbstractCLValue<?, ?>>, CLTypeOption> {
32+
3233
@JsonProperty("cl_type")
3334
private CLTypeOption clType = new CLTypeOption();
3435

35-
@JsonSetter("cl_type")
36+
@JsonCreator
37+
public CLValueOption(@JsonProperty("cl_type") final CLTypeOption clType,
38+
@JsonProperty("bytes") final String bytes,
39+
@JsonProperty("parsed") final Object parsed) {
40+
setBytes(bytes);
41+
setClType(clType);
42+
setParsed(parsed);
43+
}
44+
3645
public void setClType(final CLTypeOption clType) {
3746
this.clType = clType;
3847
childTypesSet();

src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeTuple1;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
56
import com.fasterxml.jackson.annotation.JsonProperty;
6-
import com.fasterxml.jackson.annotation.JsonSetter;
77
import dev.oak3.sbs4j.DeserializerBuffer;
88
import dev.oak3.sbs4j.SerializerBuffer;
99
import dev.oak3.sbs4j.exception.ValueSerializationException;
@@ -30,10 +30,19 @@
3030
@EqualsAndHashCode(callSuper = true)
3131
@NoArgsConstructor
3232
public class CLValueTuple1 extends AbstractCLValueWithChildren<Unit<? extends AbstractCLValue<?, ?>>, CLTypeTuple1> {
33+
3334
@JsonProperty("cl_type")
3435
private CLTypeTuple1 clType = new CLTypeTuple1();
3536

36-
@JsonSetter("cl_type")
37+
@JsonCreator
38+
public CLValueTuple1(@JsonProperty("cl_type") final CLTypeTuple1 clType,
39+
@JsonProperty("bytes") final String bytes,
40+
@JsonProperty("parsed") final Object parsed) {
41+
setBytes(bytes);
42+
setClType(clType);
43+
setParsed(parsed);
44+
}
45+
3746
public void setClType(final CLTypeTuple1 clType) {
3847
this.clType = clType;
3948
childTypesSet();

src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeTuple2;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
56
import com.fasterxml.jackson.annotation.JsonProperty;
6-
import com.fasterxml.jackson.annotation.JsonSetter;
77
import dev.oak3.sbs4j.DeserializerBuffer;
88
import dev.oak3.sbs4j.SerializerBuffer;
99
import dev.oak3.sbs4j.exception.ValueSerializationException;
@@ -31,20 +31,29 @@
3131
@NoArgsConstructor
3232
public class CLValueTuple2
3333
extends AbstractCLValueWithChildren<Pair<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeTuple2> {
34+
3435
@JsonProperty("cl_type")
3536
private CLTypeTuple2 clType = new CLTypeTuple2();
3637

37-
@JsonSetter("cl_type")
38-
public void setClType(CLTypeTuple2 clType) {
39-
this.clType = clType;
40-
childTypesSet();
38+
@JsonCreator
39+
public CLValueTuple2(@JsonProperty("cl_type") final CLTypeTuple2 clType,
40+
@JsonProperty("bytes") final String bytes,
41+
@JsonProperty("parsed") final Object parsed) {
42+
setBytes(bytes);
43+
setClType(clType);
44+
setParsed(parsed);
4145
}
4246

4347
public CLValueTuple2(Pair<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> value) throws ValueSerializationException {
4448
setChildTypes(value);
4549
this.setValue(value);
4650
}
4751

52+
public void setClType(CLTypeTuple2 clType) {
53+
this.clType = clType;
54+
childTypesSet();
55+
}
56+
4857
@Override
4958
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
5059
final SerializerBuffer serVal = new SerializerBuffer();

src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
44
import com.casper.sdk.model.clvalue.cltype.CLTypeTuple3;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
56
import com.fasterxml.jackson.annotation.JsonProperty;
6-
import com.fasterxml.jackson.annotation.JsonSetter;
77
import dev.oak3.sbs4j.DeserializerBuffer;
88
import dev.oak3.sbs4j.SerializerBuffer;
99
import dev.oak3.sbs4j.exception.ValueSerializationException;
@@ -31,20 +31,29 @@
3131
@NoArgsConstructor
3232
public class CLValueTuple3 extends
3333
AbstractCLValueWithChildren<Triplet<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeTuple3> {
34+
3435
@JsonProperty("cl_type")
3536
private CLTypeTuple3 clType = new CLTypeTuple3();
3637

37-
@JsonSetter("cl_type")
38-
public void setClType(final CLTypeTuple3 clType) {
39-
this.clType = clType;
40-
childTypesSet();
38+
@JsonCreator
39+
public CLValueTuple3(@JsonProperty("cl_type") final CLTypeTuple3 clType,
40+
@JsonProperty("bytes") final String bytes,
41+
@JsonProperty("parsed") final Object parsed) {
42+
setBytes(bytes);
43+
setClType(clType);
44+
setParsed(parsed);
4145
}
4246

4347
public CLValueTuple3(final Triplet<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>> value) throws ValueSerializationException {
4448
setChildTypes(value);
4549
this.setValue(value);
4650
}
4751

52+
public void setClType(final CLTypeTuple3 clType) {
53+
this.clType = clType;
54+
childTypesSet();
55+
}
56+
4857
@Override
4958
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
5059
final SerializerBuffer serVal = new SerializerBuffer();

src/test/java/com/casper/sdk/model/deploy/DeployDataTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ void validateDeployDataMapping_5() throws IOException, JSONException {
112112

113113
JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
114114
}
115+
115116
@Test
116117
void validateDeployDataMapping_6() throws IOException, JSONException {
117118
final String inputJson = getPrettyJson(loadJsonFromFile("deploy-samples/deploy-v6.json"));
@@ -129,6 +130,23 @@ void validateDeployDataMapping_6() throws IOException, JSONException {
129130
JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
130131
}
131132

133+
@Test
134+
void validateDeployDataMapping_7() throws IOException, JSONException {
135+
final String inputJson = getPrettyJson(loadJsonFromFile("deploy-samples/deploy-v7.json"));
136+
137+
LOGGER.debug("Original JSON: {}", inputJson);
138+
139+
final Deploy dd = OBJECT_MAPPER.readValue(inputJson, Deploy.class);
140+
141+
assertNotNull(dd);
142+
143+
final String expectedJson = getPrettyJson(dd);
144+
145+
LOGGER.debug("Serialized JSON: {}", expectedJson);
146+
147+
JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
148+
}
149+
132150
@Test
133151
void validateDeployResultMapping() throws IOException, JSONException {
134152
final String inputJson = getPrettyJson(loadJsonFromFile("deploy-samples/deploy-result.json"));
@@ -143,4 +161,4 @@ void validateDeployResultMapping() throws IOException, JSONException {
143161

144162
JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
145163
}
146-
}
164+
}

0 commit comments

Comments
 (0)