1
1
package com .fasterxml .jackson .databind .convert ;
2
2
3
- import java . util . List ;
4
-
5
- import com .fasterxml .jackson .databind .* ;
3
+ import com . fasterxml . jackson . databind . BaseMapTest ;
4
+ import com . fasterxml . jackson . databind . JavaType ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
6
import com .fasterxml .jackson .databind .cfg .CoercionAction ;
7
7
import com .fasterxml .jackson .databind .cfg .CoercionInputShape ;
8
8
import com .fasterxml .jackson .databind .exc .InvalidFormatException ;
9
+ import com .fasterxml .jackson .databind .exc .MismatchedInputException ;
10
+ import com .fasterxml .jackson .databind .type .TypeFactory ;
11
+ import java .util .List ;
9
12
10
13
public class DisableCoercions3690Test extends BaseMapTest
11
14
{
@@ -14,6 +17,18 @@ static class Input3690 {
14
17
public List <String > field ;
15
18
}
16
19
20
+ static class Input3924 <T > {
21
+ private T field ;
22
+
23
+ public T getField () {
24
+ return field ;
25
+ }
26
+
27
+ public void setField (T field ) {
28
+ this .field = field ;
29
+ }
30
+ }
31
+
17
32
// [databind#3690]
18
33
public void testCoercionFail3690 () throws Exception
19
34
{
@@ -38,4 +53,50 @@ public void testCoercionFail3690() throws Exception
38
53
verifyException (e , "to `java.lang.String` value" );
39
54
}
40
55
}
56
+
57
+ // [databind#3924]
58
+ public void testFailMessage3924 () throws Exception {
59
+ // Arrange : Building a strict ObjectMapper.
60
+ ObjectMapper mapper = jsonMapperBuilder ()
61
+ .withCoercionConfigDefaults (config -> {
62
+ config .setCoercion (CoercionInputShape .Boolean , CoercionAction .Fail )
63
+ .setCoercion (CoercionInputShape .Integer , CoercionAction .Fail )
64
+ .setCoercion (CoercionInputShape .Float , CoercionAction .Fail )
65
+ .setCoercion (CoercionInputShape .String , CoercionAction .Fail )
66
+ .setCoercion (CoercionInputShape .Array , CoercionAction .Fail )
67
+ .setCoercion (CoercionInputShape .Object , CoercionAction .Fail );
68
+ })
69
+ .build ();
70
+
71
+ // Arrange : Type configuration
72
+ TypeFactory typeFactory = mapper .getTypeFactory ();
73
+ JavaType arrayType = typeFactory .constructParametricType (List .class , String .class );
74
+ JavaType inputType = typeFactory .constructParametricType (Input3924 .class , arrayType );
75
+
76
+ // Act & Assert
77
+ _verifyFailedCoercionWithInvalidFormat ("{ \" field\" : [ 1 ] }" ,
78
+ "Cannot coerce Integer value (1) to `java.lang.String` value" ,
79
+ mapper , inputType );
80
+
81
+ _verifyFailedCoercionWithInvalidFormat ("{ \" field\" : [ [ 1 ] ] }" ,
82
+ "Cannot deserialize value of type `java.lang.String` from Array value" ,
83
+ mapper , inputType );
84
+
85
+ _verifyFailedCoercionWithInvalidFormat ("{ \" field\" : [ { \" field\" : 1 } ] }" ,
86
+ "Cannot deserialize value of type `java.lang.String` from Object value" ,
87
+ mapper , inputType );
88
+
89
+ }
90
+
91
+ private void _verifyFailedCoercionWithInvalidFormat (String jsonStr , String expectedMsg , ObjectMapper mapper ,
92
+ JavaType inputType ) throws Exception
93
+ {
94
+ try {
95
+ mapper .readValue (jsonStr , inputType );
96
+ fail ("Should not pass" );
97
+ } catch (MismatchedInputException e ) {
98
+ assertEquals (String .class , e .getTargetType ());
99
+ verifyException (e , expectedMsg );
100
+ }
101
+ }
41
102
}
0 commit comments