Skip to content

Commit a15a135

Browse files
committed
Minor test improvement wrt #3721
1 parent 7f7b8d1 commit a15a135

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/test/java/com/fasterxml/jackson/databind/deser/filter/IgnorePropertyOnDeserTest.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import com.fasterxml.jackson.databind.BaseMapTest;
99
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
11+
import com.fasterxml.jackson.databind.json.JsonMapper;
1012

1113
public class IgnorePropertyOnDeserTest extends BaseMapTest
1214
{
@@ -121,36 +123,60 @@ public void testIgnoreOnProperty1217() throws Exception
121123
assertEquals(2, result1.obj2.y);
122124
}
123125

126+
// [databind#1217]
124127
public void testIgnoreViaConfigOverride1217() throws Exception
125128
{
126-
ObjectMapper mapper = new ObjectMapper();
127-
mapper.configOverride(Point.class)
128-
.setIgnorals(JsonIgnoreProperties.Value.forIgnoredProperties("y"));
129+
ObjectMapper mapper = JsonMapper.builder()
130+
.withConfigOverride(Point.class,
131+
cfg -> cfg.setIgnorals(JsonIgnoreProperties.Value.forIgnoredProperties("y")))
132+
.build();
129133
Point p = mapper.readValue(a2q("{'x':1,'y':2}"), Point.class);
130134
// bind 'x', but ignore 'y'
131135
assertEquals(1, p.x);
132136
assertEquals(0, p.y);
133137
}
134138

139+
// [databind#3721]
140+
public void testIgnoreUnknownViaConfigOverride() throws Exception
141+
{
142+
final String DOC = a2q("{'x':2,'foobar':3}");
143+
144+
// First, fail without overrides
145+
try {
146+
MAPPER.readValue(DOC, Point.class);
147+
fail("Should not pass");
148+
} catch (UnrecognizedPropertyException e) {
149+
verifyException(e, "foobar"); // message varies between 2.x and 3.x
150+
}
151+
152+
// But pass with specific class override:
153+
ObjectMapper mapper = JsonMapper.builder()
154+
.withConfigOverride(Point.class,
155+
cfg -> cfg.setIgnorals(JsonIgnoreProperties.Value.forIgnoreUnknown(true)))
156+
.build();
157+
Point p = mapper.readValue(DOC, Point.class);
158+
assertEquals(2, p.x);
159+
160+
// 13-Jan-2023, tatu: Alas, no global defaulting yet!
161+
}
162+
135163
// [databind#1595]
136164
public void testIgnoreGetterNotSetter1595() throws Exception
137165
{
138-
ObjectMapper mapper = new ObjectMapper();
139166
Simple1595 config = new Simple1595();
140167
config.setId(123);
141168
config.setName("jack");
142-
String json = mapper.writeValueAsString(config);
169+
String json = MAPPER.writeValueAsString(config);
143170
assertEquals(a2q("{'id':123}"), json);
144-
Simple1595 des = mapper.readValue(a2q("{'id':123,'name':'jack'}"), Simple1595.class);
171+
Simple1595 des = MAPPER.readValue(a2q("{'id':123,'name':'jack'}"), Simple1595.class);
145172
assertEquals("jack", des.getName());
146173
}
147174

148175
// [databind#2627]
149176
public void testIgnoreUnknownOnField() throws IOException
150177
{
151-
ObjectMapper objectMapper = new ObjectMapper();
152178
String json = "{\"value\": {\"name\": \"my_name\", \"extra\": \"val\"}, \"type\":\"Json\"}";
153-
MyPojoValue value = objectMapper.readValue(json, MyPojoValue.class);
179+
MyPojoValue value = MAPPER.readValue(json, MyPojoValue.class);
154180
assertNotNull(value);
155181
assertNotNull(value.getValue());
156182
assertEquals("my_name", value.getValue().name);

0 commit comments

Comments
 (0)