3
3
import com .fasterxml .jackson .annotation .JsonTypeInfo ;
4
4
import com .fasterxml .jackson .databind .*;
5
5
import com .fasterxml .jackson .databind .cfg .MapperConfig ;
6
+ import com .fasterxml .jackson .databind .exc .InvalidTypeIdException ;
6
7
import com .fasterxml .jackson .databind .jsontype .PolymorphicTypeValidator ;
7
8
8
9
/**
9
- * Tests to verify working of customizable {@PolymorphicTypeValidator}
10
+ * Tests to verify working of customizable {@PolymorphicTypeValidator},
11
+ * see [databind#2195]
10
12
*
11
13
* @since 2.10
12
14
*/
@@ -28,6 +30,7 @@ public boolean equals(Object other) {
28
30
29
31
static class BadValue extends BaseValue { }
30
32
static class GoodValue extends BaseValue { }
33
+ static class MehValue extends BaseValue { }
31
34
32
35
// // // Wrapper types
33
36
@@ -54,13 +57,6 @@ protected DefTypeWrapper() { }
54
57
public DefTypeWrapper (BaseValue v ) { value = v ; }
55
58
}
56
59
57
- static class DefTypeMinimalWrapper {
58
- public BaseValue value ;
59
-
60
- protected DefTypeMinimalWrapper () { }
61
- public DefTypeMinimalWrapper (BaseValue v ) { value = v ; }
62
- }
63
-
64
60
// // // Validator implementations
65
61
66
62
static class SimpleNameBasedValidator extends PolymorphicTypeValidator {
@@ -143,12 +139,12 @@ public void testWithDefaultTypingNameAccept() throws Exception
143
139
144
140
public void testWithDefaultTypingNameDenyExplicit () throws Exception
145
141
{
146
-
142
+ _verifyBadDefaultValue ( MAPPER_DEF_TYPING_NAME_CHECK );
147
143
}
148
144
149
145
public void testWithDefaultTypingNameDenyDefault () throws Exception
150
146
{
151
-
147
+ _verifyMehDefaultValue ( MAPPER_DEF_TYPING_NAME_CHECK );
152
148
}
153
149
154
150
// // With Class check
@@ -162,12 +158,12 @@ public void testWithDefaultTypingClassAccept() throws Exception
162
158
163
159
public void testWithDefaultTypingClassDenyExplicit () throws Exception
164
160
{
165
-
161
+ _verifyBadDefaultValue ( MAPPER_DEF_TYPING_CLASS_CHECK );
166
162
}
167
163
168
164
public void testWithDefaultTypingClassDenyDefault () throws Exception
169
165
{
170
-
166
+ _verifyMehDefaultValue ( MAPPER_DEF_TYPING_CLASS_CHECK );
171
167
}
172
168
173
169
/*
@@ -187,10 +183,12 @@ public void testWithAnnotationNameAccept() throws Exception
187
183
188
184
public void testWithAnnotationNameDenyExplicit () throws Exception
189
185
{
186
+ _verifyBadAnnotatedValue (MAPPER_EXPLICIT_NAME_CHECK );
190
187
}
191
188
192
189
public void testWithAnnotationNameDenyDefault () throws Exception
193
190
{
191
+ _verifyMehAnnotatedValue (MAPPER_EXPLICIT_NAME_CHECK );
194
192
}
195
193
196
194
// // With Class
@@ -204,10 +202,12 @@ public void testWithAnnotationClassAccept() throws Exception
204
202
205
203
public void testWithAnnotationClassDenyExplicit () throws Exception
206
204
{
205
+ _verifyBadAnnotatedValue (MAPPER_EXPLICIT_CLASS_CHECK );
207
206
}
208
207
209
208
public void testWithAnnotationClassDenyDefault () throws Exception
210
209
{
210
+ _verifyMehAnnotatedValue (MAPPER_EXPLICIT_CLASS_CHECK );
211
211
}
212
212
213
213
/*
@@ -227,12 +227,12 @@ public void testWithAnnotationMinClassNameAccept() throws Exception
227
227
228
228
public void testWithAnnotationMinClassNameDenyExplicit () throws Exception
229
229
{
230
-
230
+ _verifyBadAnnotatedMinValue ( MAPPER_EXPLICIT_NAME_CHECK );
231
231
}
232
232
233
233
public void testWithAnnotationMinClassNameDenyDefault () throws Exception
234
234
{
235
-
235
+ _verifyMehAnnotatedMinValue ( MAPPER_EXPLICIT_NAME_CHECK );
236
236
}
237
237
238
238
// // With Class
@@ -246,17 +246,17 @@ public void testWithAnnotationMinClassClassAccept() throws Exception
246
246
247
247
public void testWithAnnotationMinClassClassDenyExplicit () throws Exception
248
248
{
249
-
249
+ _verifyBadAnnotatedMinValue ( MAPPER_EXPLICIT_CLASS_CHECK );
250
250
}
251
251
252
252
public void testWithAnnotationMinClassClassDenyDefault () throws Exception
253
253
{
254
-
254
+ _verifyMehAnnotatedMinValue ( MAPPER_EXPLICIT_CLASS_CHECK );
255
255
}
256
256
257
257
/*
258
258
/**********************************************************************
259
- /* Helper methods
259
+ /* Helper methods, round-trip (ok case)
260
260
/**********************************************************************
261
261
*/
262
262
@@ -274,4 +274,62 @@ private AnnotatedMinimalWrapper _roundTripAnnotatedMinimal(ObjectMapper mapper,
274
274
final String json = mapper .writeValueAsString (new AnnotatedMinimalWrapper (input ));
275
275
return mapper .readValue (json , AnnotatedMinimalWrapper .class );
276
276
}
277
+
278
+ /*
279
+ /**********************************************************************
280
+ /* Helper methods, failing deser verification
281
+ /**********************************************************************
282
+ */
283
+
284
+ private void _verifyBadDefaultValue (ObjectMapper mapper ) throws Exception {
285
+ final String json = mapper .writeValueAsString (new DefTypeWrapper (new BadValue ()));
286
+ _verifyBadValue (mapper , json , DefTypeWrapper .class );
287
+ }
288
+
289
+ private void _verifyMehDefaultValue (ObjectMapper mapper ) throws Exception {
290
+ final String json = mapper .writeValueAsString (new DefTypeWrapper (new MehValue ()));
291
+ _verifyMehValue (mapper , json , DefTypeWrapper .class );
292
+ }
293
+
294
+ private void _verifyBadAnnotatedValue (ObjectMapper mapper ) throws Exception {
295
+ final String json = mapper .writeValueAsString (new AnnotatedWrapper (new BadValue ()));
296
+ _verifyBadValue (mapper , json , AnnotatedWrapper .class );
297
+ }
298
+
299
+ private void _verifyMehAnnotatedValue (ObjectMapper mapper ) throws Exception {
300
+ final String json = mapper .writeValueAsString (new AnnotatedWrapper (new MehValue ()));
301
+ _verifyMehValue (mapper , json , AnnotatedWrapper .class );
302
+ }
303
+
304
+ private void _verifyBadAnnotatedMinValue (ObjectMapper mapper ) throws Exception {
305
+ final String json = mapper .writeValueAsString (new AnnotatedMinimalWrapper (new BadValue ()));
306
+ _verifyBadValue (mapper , json , AnnotatedMinimalWrapper .class );
307
+ }
308
+
309
+ private void _verifyMehAnnotatedMinValue (ObjectMapper mapper ) throws Exception {
310
+ final String json = mapper .writeValueAsString (new AnnotatedMinimalWrapper (new MehValue ()));
311
+ _verifyMehValue (mapper , json , AnnotatedMinimalWrapper .class );
312
+ }
313
+
314
+ private void _verifyBadValue (ObjectMapper mapper , String json , Class <?> type ) throws Exception {
315
+ try {
316
+ mapper .readValue (json , type );
317
+ fail ("Should not pass" );
318
+ } catch (InvalidTypeIdException e ) {
319
+ verifyException (e , "Could not resolve type id" );
320
+ verifyException (e , "`PolymorphicTypeValidator`" );
321
+ verifyException (e , "denied resolution" );
322
+ }
323
+ }
324
+
325
+ private void _verifyMehValue (ObjectMapper mapper , String json , Class <?> type ) throws Exception {
326
+ try {
327
+ mapper .readValue (json , type );
328
+ fail ("Should not pass" );
329
+ } catch (InvalidTypeIdException e ) {
330
+ verifyException (e , "Could not resolve type id" );
331
+ verifyException (e , "`PolymorphicTypeValidator`" );
332
+ verifyException (e , "denied resolution" );
333
+ }
334
+ }
277
335
}
0 commit comments