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

check added to prevent IonValue null deserialization to fail when bean has a missing property #322

Merged
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 @@ -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
Original file line number Diff line number Diff line change
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