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
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ public String[] deserialize(JsonParser p, DeserializationContext ctxt) throws IO
}
String[] result = buffer.completeAndClearBuffer(chunk, ix, String.class);
ctxt.returnObjectBuffer(buffer);
if (result != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can never be null, let's remove check.

if (result.length == 0) {
// [databind#4949]: Coercion from empty array not applied
result = coerceEmptyArray(result, ctxt);
}
}
return result;
}

private String[] coerceEmptyArray(String[] result, DeserializationContext ctxt)
throws JsonMappingException
{
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,37 @@
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.assertEquals;
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 Pojo4994 {
public String[] value;
}

@Test
public void testAsNull()
throws Exception
{
final ObjectMapper MAPPER_TO_NULL = jsonMapperBuilder()
.withCoercionConfigDefaults(cfg ->
cfg.setCoercion(CoercionInputShape.EmptyArray, CoercionAction.AsNull))
.build();

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

Pojo4994 pojo = MAPPER_TO_NULL.readValue(json, Pojo4994.class);

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

}