1
- package com .fasterxml .jackson .failing ;
2
-
3
- import java .util .regex .Pattern ;
1
+ package com .fasterxml .jackson .databind .jsontype .vld ;
4
2
5
3
import com .fasterxml .jackson .databind .*;
6
4
import com .fasterxml .jackson .databind .DefaultTyping ;
7
- import com .fasterxml .jackson .databind .exc .InvalidDefinitionException ;
8
5
import com .fasterxml .jackson .databind .exc .InvalidTypeIdException ;
9
6
import com .fasterxml .jackson .databind .jsontype .BasicPolymorphicTypeValidator ;
10
7
import com .fasterxml .jackson .databind .jsontype .PolymorphicTypeValidator ;
11
8
12
9
/**
13
- * Tests for the main user-configurable {@code PolymorphicTypeValidator},
14
- * {@link BasicPolymorphicTypeValidator}.
10
+ * Tests related to [databind#2534], support for configuring
11
+ * {@link BasicPolymorphicTypeValidator} to allow all Array-valued
12
+ * polymorphic values.
15
13
*/
16
- public class BasicPTV2532Test extends BaseMapTest
14
+ public class BasicPTVWithArraysTest extends BaseMapTest
17
15
{
18
- // // // Value types
19
-
20
- static abstract class Base2532 {
16
+ static abstract class Base2534 {
21
17
public int x = 3 ;
22
18
}
23
19
24
- static class Good2532 extends Base2532 {
25
- protected Good2532 () { }
26
- public Good2532 (int x ) {
20
+ static class Good2534 extends Base2534 {
21
+ protected Good2534 () { }
22
+ public Good2534 (int x ) {
27
23
super ();
28
24
this .x = x ;
29
25
}
30
26
}
31
27
32
- static class Bad2532 extends Base2532 {
33
- protected Bad2532 () { }
34
- public Bad2532 (int x ) {
28
+ static class Bad2534 extends Base2534 {
29
+ protected Bad2534 () { }
30
+ public Bad2534 (int x ) {
35
31
super ();
36
32
this .x = x ;
37
33
}
@@ -50,19 +46,17 @@ protected ObjectWrapper() { }
50
46
/**********************************************************************
51
47
*/
52
48
53
- // [databind#2532 ]: handle Java array-types appropriately wrt validation
49
+ // [databind#2534 ]: handle Java array-types appropriately wrt validation
54
50
public void testAllowBySubClassInArray () throws Exception {
55
51
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator .builder ()
56
- .allowIfSubType (Good2532 .class )
52
+ .allowIfSubType (Good2534 .class )
57
53
.build ();
58
54
ObjectMapper mapper = jsonMapperBuilder ()
59
55
.activateDefaultTyping (ptv , DefaultTyping .NON_FINAL )
60
56
.build ();
61
57
62
- final ObjectWrapper input = new ObjectWrapper (new Base2532 [] { new Good2532 (42 ) });
63
- final String json = mapper .writeValueAsString (input );
58
+ final String json = mapper .writeValueAsString (new ObjectWrapper (new Base2534 [] { new Good2534 (42 ) }));
64
59
65
- System .err .println ("JSON: " +json );
66
60
// First test blocked case:
67
61
try {
68
62
mapper .readValue (json , ObjectWrapper .class );
@@ -74,7 +68,8 @@ public void testAllowBySubClassInArray() throws Exception {
74
68
75
69
// and then accepted:
76
70
ptv = BasicPolymorphicTypeValidator .builder ()
77
- .allowIfSubType (Good2532 .class )
71
+ .allowIfSubTypeIsArray () // key addition
72
+ .allowIfSubType (Good2534 .class )
78
73
.build ();
79
74
mapper = jsonMapperBuilder ()
80
75
.activateDefaultTyping (ptv , DefaultTyping .NON_FINAL )
@@ -83,10 +78,20 @@ public void testAllowBySubClassInArray() throws Exception {
83
78
ObjectWrapper w = mapper .readValue (json , ObjectWrapper .class );
84
79
assertNotNull (w );
85
80
assertNotNull (w .value );
86
- assertEquals (Base2532 [].class , w .value .getClass ());
87
- Base2532 [] arrayOut = (Base2532 []) w .value ;
81
+ assertEquals (Base2534 [].class , w .value .getClass ());
82
+ Base2534 [] arrayOut = (Base2534 []) w .value ;
88
83
assertEquals (1 , arrayOut .length );
89
-
90
84
assertEquals (42 , arrayOut [0 ].x );
85
+
86
+ // but ensure array-acceptance does NOT allow non-validated element types!
87
+ final String badJson = mapper .writeValueAsString (new ObjectWrapper (new Base2534 [] { new Bad2534 (13 ) }));
88
+ try {
89
+ mapper .readValue (badJson , ObjectWrapper .class );
90
+ fail ("Should not pass" );
91
+ } catch (InvalidTypeIdException e ) {
92
+ verifyException (e , "Could not resolve type id 'com.fasterxml.jackson." );
93
+ verifyException (e , "$Bad2534" );
94
+ verifyException (e , "as a subtype of" );
95
+ }
91
96
}
92
97
}
0 commit comments