Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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. #275

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
@EqualsAndHashCode(callSuper = true)
public abstract class AbstractCLValueWithChildren<T, P extends AbstractCLTypeWithChildren> extends AbstractCLValue<T, P> {

protected abstract void setChildTypes(T value);

/**
Expand All @@ -30,7 +31,7 @@ public abstract class AbstractCLValueWithChildren<T, P extends AbstractCLTypeWit
*/
@SneakyThrows({ValueDeserializationException.class})
protected void childTypesSet() {
if (!getBytes().isEmpty()) {
if (!getBytes().isEmpty() && getClType().isDeserializable()) {
this.deserialize(new DeserializerBuffer(this.getBytes()));
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueAny.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
import com.casper.sdk.model.clvalue.cltype.CLTypeAny;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
Expand All @@ -28,8 +30,19 @@
@Setter
@NoArgsConstructor
public class CLValueAny extends AbstractCLValue<byte[], CLTypeAny> {

private CLTypeAny clType = new CLTypeAny();

@JsonCreator
public CLValueAny(@JsonProperty("cl_type") final CLTypeAny clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final Object parsed) throws ValueSerializationException {
setBytes(bytes);
setClType(clType);
setValue(Hex.decode(bytes));
setParsed(parsed);
}

@JsonSetter("cl_type")
@ExcludeFromJacocoGeneratedReport
protected void setJsonClType(final CLTypeAny clType) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.casper.sdk.annotation.ExcludeFromJacocoGeneratedReport;
import com.casper.sdk.model.clvalue.cltype.CLTypeByteArray;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
Expand All @@ -27,9 +28,19 @@
@Setter
@NoArgsConstructor
public class CLValueByteArray extends AbstractCLValue<byte[], CLTypeByteArray> {

@JsonProperty("cl_type")
private CLTypeByteArray clType = new CLTypeByteArray();

@JsonCreator
public CLValueByteArray(@JsonProperty("cl_type") final CLTypeByteArray clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final String parsed) throws ValueSerializationException {
this.clType = clType;
setValue(Hex.decode(bytes));
setParsed(parsed);
}

public CLValueByteArray(final byte[] value) throws ValueSerializationException {
this.setValue(value);
this.clType.setLength(value.length);
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.casper.sdk.model.clvalue.cltype.CLTypeData;
import com.casper.sdk.model.clvalue.cltype.CLTypeList;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
Expand All @@ -30,20 +30,29 @@
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class CLValueList extends AbstractCLValueWithChildren<List<? extends AbstractCLValue<?, ?>>, CLTypeList> {

@JsonProperty("cl_type")
private CLTypeList clType = new CLTypeList();

@JsonSetter("cl_type")
public void setClType(final CLTypeList clType) {
this.clType = clType;
childTypesSet();
@JsonCreator
public CLValueList(@JsonProperty("cl_type") final CLTypeList clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final Object parsed) {
setBytes(bytes);
setClType(clType);
setParsed(parsed);
}

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

public void setClType(final CLTypeList clType) {
this.clType = clType;
childTypesSet();
}

@Override
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.casper.sdk.model.clvalue.cltype.CLTypeData;
import com.casper.sdk.model.clvalue.cltype.CLTypeMap;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
Expand Down Expand Up @@ -33,10 +33,19 @@
@NoArgsConstructor
public class CLValueMap extends
AbstractCLValueWithChildren<Map<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeMap> {

@JsonProperty("cl_type")
private CLTypeMap clType = new CLTypeMap();

@JsonSetter("cl_type")
@JsonCreator
public CLValueMap(@JsonProperty("cl_type") final CLTypeMap clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final Object parsed) {
setBytes(bytes);
setClType(clType);
setParsed(parsed);
}

public void setClType(CLTypeMap clType) {
this.clType = clType;
childTypesSet();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.casper.sdk.model.clvalue.cltype.AbstractCLTypeWithChildren;
import com.casper.sdk.model.clvalue.cltype.CLTypeData;
import com.casper.sdk.model.clvalue.cltype.CLTypeOption;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
Expand All @@ -29,10 +29,19 @@
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class CLValueOption extends AbstractCLValueWithChildren<Optional<AbstractCLValue<?, ?>>, CLTypeOption> {

@JsonProperty("cl_type")
private CLTypeOption clType = new CLTypeOption();

@JsonSetter("cl_type")
@JsonCreator
public CLValueOption(@JsonProperty("cl_type") final CLTypeOption clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final Object parsed) {
setBytes(bytes);
setClType(clType);
setParsed(parsed);
}

public void setClType(final CLTypeOption clType) {
this.clType = clType;
childTypesSet();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueTuple1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.casper.sdk.model.clvalue.cltype.CLTypeData;
import com.casper.sdk.model.clvalue.cltype.CLTypeTuple1;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
Expand All @@ -30,10 +30,19 @@
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class CLValueTuple1 extends AbstractCLValueWithChildren<Unit<? extends AbstractCLValue<?, ?>>, CLTypeTuple1> {

@JsonProperty("cl_type")
private CLTypeTuple1 clType = new CLTypeTuple1();

@JsonSetter("cl_type")
@JsonCreator
public CLValueTuple1(@JsonProperty("cl_type") final CLTypeTuple1 clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final Object parsed) {
setBytes(bytes);
setClType(clType);
setParsed(parsed);
}

public void setClType(final CLTypeTuple1 clType) {
this.clType = clType;
childTypesSet();
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueTuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.casper.sdk.model.clvalue.cltype.CLTypeData;
import com.casper.sdk.model.clvalue.cltype.CLTypeTuple2;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
Expand Down Expand Up @@ -31,20 +31,29 @@
@NoArgsConstructor
public class CLValueTuple2
extends AbstractCLValueWithChildren<Pair<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeTuple2> {

@JsonProperty("cl_type")
private CLTypeTuple2 clType = new CLTypeTuple2();

@JsonSetter("cl_type")
public void setClType(CLTypeTuple2 clType) {
this.clType = clType;
childTypesSet();
@JsonCreator
public CLValueTuple2(@JsonProperty("cl_type") final CLTypeTuple2 clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final Object parsed) {
setBytes(bytes);
setClType(clType);
setParsed(parsed);
}

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

public void setClType(CLTypeTuple2 clType) {
this.clType = clType;
childTypesSet();
}

@Override
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer serVal = new SerializerBuffer();
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/casper/sdk/model/clvalue/CLValueTuple3.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.casper.sdk.model.clvalue.cltype.CLTypeData;
import com.casper.sdk.model.clvalue.cltype.CLTypeTuple3;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import dev.oak3.sbs4j.DeserializerBuffer;
import dev.oak3.sbs4j.SerializerBuffer;
import dev.oak3.sbs4j.exception.ValueSerializationException;
Expand Down Expand Up @@ -31,20 +31,29 @@
@NoArgsConstructor
public class CLValueTuple3 extends
AbstractCLValueWithChildren<Triplet<? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>, ? extends AbstractCLValue<?, ?>>, CLTypeTuple3> {

@JsonProperty("cl_type")
private CLTypeTuple3 clType = new CLTypeTuple3();

@JsonSetter("cl_type")
public void setClType(final CLTypeTuple3 clType) {
this.clType = clType;
childTypesSet();
@JsonCreator
public CLValueTuple3(@JsonProperty("cl_type") final CLTypeTuple3 clType,
@JsonProperty("bytes") final String bytes,
@JsonProperty("parsed") final Object parsed) {
setBytes(bytes);
setClType(clType);
setParsed(parsed);
}

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

public void setClType(final CLTypeTuple3 clType) {
this.clType = clType;
childTypesSet();
}

@Override
protected void serializeValue(final SerializerBuffer ser) throws ValueSerializationException {
final SerializerBuffer serVal = new SerializerBuffer();
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/com/casper/sdk/model/deploy/DeployDataTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void validateDeployDataMapping_5() throws IOException, JSONException {

JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
void validateDeployDataMapping_6() throws IOException, JSONException {
final String inputJson = getPrettyJson(loadJsonFromFile("deploy-samples/deploy-v6.json"));
Expand All @@ -129,6 +130,23 @@ void validateDeployDataMapping_6() throws IOException, JSONException {
JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
void validateDeployDataMapping_7() throws IOException, JSONException {
final String inputJson = getPrettyJson(loadJsonFromFile("deploy-samples/deploy-v7.json"));

LOGGER.debug("Original JSON: {}", inputJson);

final Deploy dd = OBJECT_MAPPER.readValue(inputJson, Deploy.class);

assertNotNull(dd);

final String expectedJson = getPrettyJson(dd);

LOGGER.debug("Serialized JSON: {}", expectedJson);

JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
void validateDeployResultMapping() throws IOException, JSONException {
final String inputJson = getPrettyJson(loadJsonFromFile("deploy-samples/deploy-result.json"));
Expand All @@ -143,4 +161,4 @@ void validateDeployResultMapping() throws IOException, JSONException {

JSONAssert.assertEquals(inputJson, expectedJson, JSONCompareMode.NON_EXTENSIBLE);
}
}
}
Loading
Loading