Skip to content

Commit

Permalink
check added to prevent IonValue null deserialization to fail when bea…
Browse files Browse the repository at this point in the history
…n has a missing property (#322)
  • Loading branch information
atokuzAmzn committed Apr 25, 2022
1 parent 8347d96 commit c13fd35
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonDeserializer;
Expand Down Expand Up @@ -58,11 +59,14 @@ public IonValue deserialize(JsonParser jp, DeserializationContext ctxt) throws I
@Override
public IonValue getNullValue(DeserializationContext ctxt) throws JsonMappingException {
try {
Object embeddedObj = ctxt.getParser().getEmbeddedObject();
if (embeddedObj instanceof IonValue) {
IonValue iv = (IonValue) embeddedObj;
if (iv.isNullValue()) {
return iv;
final JsonParser parser = ctxt.getParser();
if (parser != null && parser.getCurrentToken() != JsonToken.END_OBJECT) {
final Object embeddedObj = parser.getEmbeddedObject();
if (embeddedObj instanceof IonValue) {
IonValue iv = (IonValue) embeddedObj;
if (iv.isNullValue()) {
return iv;
}
}
}

Expand Down
Expand Up @@ -2,12 +2,16 @@

import com.amazon.ion.IonSystem;
import com.amazon.ion.IonValue;
import com.amazon.ion.IonStruct;
import com.amazon.ion.system.IonSystemBuilder;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.util.AccessPattern;
import com.fasterxml.jackson.dataformat.ion.IonObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -166,6 +170,39 @@ public void shouldBeAbleToSerializeAndDeserializeStringData() throws Exception {
assertEquals(source, result);
}

static class MyBean {
public IonStruct required;
public IonStruct optional;

MyBean(
@JsonProperty("required") IonStruct required,
@JsonProperty("optional") IonStruct optional
) {
this.required = required;
this.optional = optional;
}
}

@Test
public void testWithMissingProperty() throws IOException
{
IonSystem ionSystem = IonSystemBuilder.standard().build();
IonObjectMapper ionObjectMapper = IonObjectMapper.builder(ionSystem)
.addModule(new IonValueModule())
.build();

String input1 = "{required:{}, optional:{}}";
MyBean deserializedBean1 = ionObjectMapper.readValue(input1, MyBean.class);
assertEquals(ionSystem.newEmptyStruct(), deserializedBean1.required);
assertEquals(ionSystem.newEmptyStruct(), deserializedBean1.optional);

// This deserialization should not fail with missing property
String input2 = "{required:{}}";
MyBean deserializedBean2 = ionObjectMapper.readValue(input2, MyBean.class);
assertEquals(ionSystem.newEmptyStruct(), deserializedBean2.required);
assertEquals(null, deserializedBean2.optional);
}

@Test
public void shouldOverrideNullAccessPatternToBeDynamic() {
IonValueDeserializer deserializer = new IonValueDeserializer();
Expand Down

0 comments on commit c13fd35

Please sign in to comment.