9
9
import com .fasterxml .jackson .core .JsonProcessingException ;
10
10
11
11
import com .fasterxml .jackson .databind .*;
12
+ import com .fasterxml .jackson .databind .exc .UnrecognizedPropertyException ;
12
13
13
14
public class CaseInsensitiveDeserTest extends BaseMapTest
14
15
{
@@ -93,13 +94,31 @@ public String getId() {
93
94
}
94
95
}
95
96
97
+ // [databind#1886]: allow case-insensitivity by default on a class
98
+ @ JsonFormat (with ={ JsonFormat .Feature .ACCEPT_CASE_INSENSITIVE_PROPERTIES })
99
+ static class CaseInsensitiveRole {
100
+ public String ID ;
101
+ public String Name ;
102
+ }
103
+
104
+ // [databind#1886]: allow case-insensitivity by default on a class
105
+ static class CaseInsensitiveRoleContainer {
106
+ public CaseInsensitiveRole role ;
107
+ }
108
+
109
+ // [databind#1886]: ... but also overrides
110
+ static class CaseSensitiveRoleContainer {
111
+ @ JsonFormat (without ={ JsonFormat .Feature .ACCEPT_CASE_INSENSITIVE_PROPERTIES })
112
+ public CaseInsensitiveRole role ;
113
+ }
114
+
96
115
/*
97
116
/********************************************************
98
117
/* Test methods
99
118
/********************************************************
100
119
*/
101
120
102
- private final ObjectMapper MAPPER = new ObjectMapper ();
121
+ private final ObjectMapper MAPPER = newJsonMapper ();
103
122
private final ObjectMapper INSENSITIVE_MAPPER = jsonMapperBuilder ()
104
123
.enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_PROPERTIES )
105
124
.build ();
@@ -158,7 +177,7 @@ public void testCreatorWithInsensitive() throws Exception
158
177
}
159
178
160
179
// And allow config overrides too
161
- public void testCaseInsensitiveWithClassFormat () throws Exception
180
+ public void testCaseInsensitiveViaConfigOverride () throws Exception
162
181
{
163
182
ObjectMapper mapper = new ObjectMapper ();
164
183
mapper .configOverride (Role .class )
@@ -181,4 +200,34 @@ public void testIssue1854() throws Exception
181
200
assertNotNull (result .getItems ());
182
201
assertEquals (1 , result .getItems ().size ());
183
202
}
203
+
204
+
205
+ // [databind#1886]: allow case-insensitivity by default on a class
206
+ public void testCaseInsensitiveViaClassAnnotation () throws Exception
207
+ {
208
+ final String CONTAINED = aposToQuotes ("{'role': {'id':'3','name':'Bob'}}" );
209
+
210
+ // First: via wrapper/container:
211
+ CaseInsensitiveRoleContainer cont = MAPPER .readValue (CONTAINED ,
212
+ CaseInsensitiveRoleContainer .class );
213
+ assertEquals ("3" , cont .role .ID );
214
+ assertEquals ("Bob" , cont .role .Name );
215
+
216
+ // second: directly as root value
217
+ CaseInsensitiveRole role = MAPPER .readValue
218
+ (aposToQuotes ("{'id':'12','name':'Billy'}" ),
219
+ CaseInsensitiveRole .class );
220
+ assertEquals ("12" , role .ID );
221
+ assertEquals ("Billy" , role .Name );
222
+
223
+ // and finally, more complicated; should be possible to force sensitivity:
224
+ try {
225
+ /*CaseSensitiveRoleContainer r =*/ MAPPER .readValue (CONTAINED ,
226
+ CaseSensitiveRoleContainer .class );
227
+ fail ("Should not pass" );
228
+ } catch (UnrecognizedPropertyException e ) {
229
+ verifyException (e , "Unrecognized " );
230
+ verifyException (e , "\" id\" " );
231
+ }
232
+ }
184
233
}
0 commit comments