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

Fix #4994 CoercionConfig for CoercionAction.AsNull not working on empty array deserialization #4999

Closed
wants to merge 9 commits into from
Closed
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Project: jackson-databind
(reported by @nlisker)
#4932: Conversion of `MissingNode` throws `JsonProcessingException`
(reported by @ludgerb)
#4994: `CoercionConfig` for `CoercionAction.AsNull` not working on empty array deserialization
(reported by @Whi7y)
(fix by Joo-Hyuk K)

2.18.2 (27-Nov-2024)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonFormat;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
Expand Down Expand Up @@ -237,6 +236,19 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt)
result = buffer.completeAndClearBuffer(chunk, ix, _elementClass);
}
ctxt.returnObjectBuffer(buffer);
// [databind#4949]: Coercion from empty array not applied
if (result.length == 0) {
result = coerceEmptyArray(result, ctxt);
}
return result;
}

private Object[] coerceEmptyArray(Object[] result, DeserializationContext ctxt)
{
final CoercionAction act = ctxt.findCoercionAction(logicalType(), handledType(), CoercionInputShape.EmptyArray);
if (act == CoercionAction.AsNull) {
return null;
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ public String[] deserialize(JsonParser p, DeserializationContext ctxt) throws IO
}
String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
ctxt.returnObjectBuffer(buffer);
// [databind#4949]: Coercion from empty array not applied
if (result.length == 0) {
result = coerceEmptyArray(result, ctxt);
}
return result;
}

private String[] coerceEmptyArray(String[] result, DeserializationContext ctxt)
{
final CoercionAction act = ctxt.findCoercionAction(logicalType(), handledType(), CoercionInputShape.EmptyArray);
if (act == CoercionAction.AsNull) {
return null;
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fasterxml.jackson.databind.convert;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertNull;

// [databind#4994] CoercionConfig for empty arrays not working as expected
public class CoerceEmptyArrayAsNull4994Test
extends DatabindTestUtil
{
public static class StringArrayWrapper4994 {
public String[] value;
}

public static class ObjectArrayWrapper4994 {
public Object[] value;
}

public static class PojoArrayWrapper4994 {
public Pojo4994[] value;
}

public static class Pojo4994 {
public String name;
public int age;
}

private final String json = "{\"value\": []}";

private final ObjectMapper MAPPER_TO_NULL = jsonMapperBuilder()
.withCoercionConfigDefaults(cfg ->
cfg.setCoercion(CoercionInputShape.EmptyArray, CoercionAction.AsNull))
.build();

@Test
public void testStringArrayAsNullCoercion()
throws Exception
{
StringArrayWrapper4994 pojo = MAPPER_TO_NULL.readValue(json, StringArrayWrapper4994.class);

assertNull(pojo.value); // expected: <null> but was: <[]>
}

@Test
public void testPojoArrayAsNullCoercion()
throws Exception
{
PojoArrayWrapper4994 wrapper = MAPPER_TO_NULL.readValue(json, PojoArrayWrapper4994.class);

assertNull(wrapper.value);
}

@Test
public void testObjectArrayAsNullCoercion()
throws Exception
{
ObjectArrayWrapper4994 wrapper = MAPPER_TO_NULL.readValue(json, ObjectArrayWrapper4994.class);

assertNull(wrapper.value);
}

}