Skip to content

Commit b66dfb6

Browse files
committed
Add a failing test for #2974
1 parent 093cdcb commit b66dfb6

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/test-jdk14/java/com/fasterxml/jackson/databind/RecordTest.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.fasterxml.jackson.databind;
22

3-
import com.fasterxml.jackson.annotation.JsonCreator;
4-
import com.fasterxml.jackson.annotation.JsonIgnore;
5-
import com.fasterxml.jackson.annotation.JsonProperty;
3+
import com.fasterxml.jackson.annotation.*;
64

75
import com.fasterxml.jackson.databind.json.JsonMapper;
86
import com.fasterxml.jackson.databind.util.ClassUtil;
@@ -148,18 +146,24 @@ public void testDeserializeWithAltCtor() throws Exception {
148146
assertEquals("name2", value.name());
149147
}
150148

151-
public void testSerializeJsonRenameRecord() throws Exception {
149+
public void testSerializeJsonRename() throws Exception {
152150
String json = MAPPER.writeValueAsString(new RecordWithRename(123, "Bob"));
153151
final Object EXP = map("id", Integer.valueOf(123), "rename", "Bob");
154152
assertEquals(EXP, MAPPER.readValue(json, Object.class));
155153
}
156154

157-
public void testDeserializeJsonRenameRecord() throws Exception {
155+
public void testDeserializeJsonRename() throws Exception {
158156
RecordWithRename value = MAPPER.readValue("{\"id\":123,\"rename\":\"Bob\"}",
159157
RecordWithRename.class);
160158
assertEquals(new RecordWithRename(123, "Bob"), value);
161159
}
162160

161+
/*
162+
/**********************************************************************
163+
/* Internal helper methods
164+
/**********************************************************************
165+
*/
166+
163167
private Map<String,Object> map(String key1, Object value1,
164168
String key2, Object value2) {
165169
final Map<String, Object> result = new LinkedHashMap<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fasterxml.jackson.databind;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import com.fasterxml.jackson.annotation.JsonSetter;
7+
import com.fasterxml.jackson.annotation.Nulls;
8+
import com.fasterxml.jackson.databind.RecordTest.RecordWithRename;
9+
import com.fasterxml.jackson.databind.exc.InvalidNullException;
10+
11+
public class RecordWithJsonSetter2974Test extends BaseMapTest
12+
{
13+
record RecordWithNonNullDefs(@JsonSetter(nulls=Nulls.AS_EMPTY) List<String> names,
14+
@JsonSetter(nulls=Nulls.FAIL) Map<String, Integer> agesByNames)
15+
{ }
16+
17+
private final ObjectMapper MAPPER = newJsonMapper();
18+
19+
/*
20+
/**********************************************************************
21+
/* Test methods, Record type introspection
22+
/**********************************************************************
23+
*/
24+
25+
// [databind#2974]
26+
public void testDeserializeWithNullAsEmpty() throws Exception
27+
{
28+
final ObjectReader r = MAPPER.readerFor(RecordWithNonNullDefs.class);
29+
// First, regular case
30+
RecordWithNonNullDefs value = r.readValue(a2q(
31+
"{'names':['bob'],'agesByNames':{'bob':39}}"));
32+
assertEquals(1, value.names().size());
33+
assertEquals("bob", value.names().get(0));
34+
assertEquals(1, value.agesByNames().size());
35+
assertEquals(Integer.valueOf(39), value.agesByNames().get("bob"));
36+
37+
// Then leave out list
38+
value = r.readValue(a2q("{'agesByNames':{'bob':42}}"));
39+
assertNotNull(value.names());
40+
assertEquals(0, value.names().size());
41+
assertNotNull(value.agesByNames());
42+
assertEquals(1, value.agesByNames().size());
43+
assertEquals(Integer.valueOf(42), value.agesByNames().get("bob"));
44+
45+
assertEquals(new RecordWithRename(123, "Bob"), value);
46+
}
47+
48+
// [databind#2974]
49+
public void testDeserializeWithFailForNull() throws Exception
50+
{
51+
final ObjectReader r = MAPPER.readerFor(RecordWithNonNullDefs.class);
52+
// First, regular case
53+
// But attempting to leave out Map ought to fail
54+
try {
55+
/*RecordWithNonNullDefs value =*/ r.readValue(a2q("{'names':['bob']}"));
56+
fail("Should not pass with missing/null 'agesByNames'");
57+
} catch (InvalidNullException e) {
58+
verifyException(e, "property \"agesByNames\"");
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)