@@ -76,6 +76,84 @@ public void testEnumFromBlankPhysicalTypeConfig() throws Exception {
76
76
/********************************************************
77
77
*/
78
78
79
+ public void testEnumFromIntFailLegacy () throws Exception
80
+ {
81
+ final ObjectReader r = MAPPER .readerFor (EnumCoerce .class );
82
+
83
+ // by default, should be ok
84
+ EnumCoerce result = r .readValue ("1" );
85
+ assertEquals (EnumCoerce .values ()[1 ], result );
86
+
87
+ try {
88
+ r .with (DeserializationFeature .FAIL_ON_NUMBERS_FOR_ENUMS )
89
+ .readValue ("1" );
90
+ fail ("Should not pass" );
91
+ } catch (Exception e ) {
92
+ verifyException (e , "not allowed to deserialize Enum value out of" );
93
+ }
94
+ }
95
+
96
+ public void testEnumFromIntAsNull () throws Exception
97
+ {
98
+ final String json = "1" ;
99
+ ObjectMapper mapper ;
100
+
101
+ mapper = _globMapper (CoercionInputShape .Integer , CoercionAction .AsNull , false );
102
+ assertNull (_readEnumPass (mapper , json ));
103
+ mapper = _logMapper (LogicalType .Enum ,
104
+ CoercionInputShape .Integer , CoercionAction .AsNull , false );
105
+ assertNull (_readEnumPass (mapper , json ));
106
+ mapper = _physMapper (EnumCoerce .class ,
107
+ CoercionInputShape .Integer , CoercionAction .AsNull , false );
108
+ assertNull (_readEnumPass (mapper , json ));
109
+ }
110
+
111
+ public void testEnumFromIntAsEmpty () throws Exception
112
+ {
113
+ final String json = "1" ;
114
+ ObjectMapper mapper ;
115
+
116
+ mapper = _globMapper (CoercionInputShape .Integer , CoercionAction .AsEmpty , false );
117
+ assertEquals (ENUM_DEFAULT , _readEnumPass (mapper , json ));
118
+ mapper = _logMapper (LogicalType .Enum ,
119
+ CoercionInputShape .Integer , CoercionAction .AsEmpty , false );
120
+ assertEquals (ENUM_DEFAULT , _readEnumPass (mapper , json ));
121
+ mapper = _physMapper (EnumCoerce .class ,
122
+ CoercionInputShape .Integer , CoercionAction .AsEmpty , false );
123
+ assertEquals (ENUM_DEFAULT , _readEnumPass (mapper , json ));
124
+ }
125
+
126
+ public void testEnumFromIntCoerce () throws Exception
127
+ {
128
+ final String json = "1" ;
129
+ ObjectMapper mapper ;
130
+ EnumCoerce exp = EnumCoerce .B ; // entry[1]
131
+
132
+ mapper = _globMapper (CoercionInputShape .Integer , CoercionAction .TryConvert , false );
133
+ assertEquals (exp , _readEnumPass (mapper , json ));
134
+ mapper = _logMapper (LogicalType .Enum ,
135
+ CoercionInputShape .Integer , CoercionAction .TryConvert , false );
136
+ assertEquals (exp , _readEnumPass (mapper , json ));
137
+ mapper = _physMapper (EnumCoerce .class ,
138
+ CoercionInputShape .Integer , CoercionAction .TryConvert , false );
139
+ assertEquals (exp , _readEnumPass (mapper , json ));
140
+ }
141
+
142
+ public void testEnumFromIntFailCoercionConfig () throws Exception
143
+ {
144
+ final String json = "1" ;
145
+ ObjectMapper mapper ;
146
+
147
+ mapper = _globMapper (CoercionInputShape .Integer , CoercionAction .Fail , false );
148
+ _verifyFromIntegerFail (mapper , json );
149
+ mapper = _logMapper (LogicalType .Enum ,
150
+ CoercionInputShape .Integer , CoercionAction .Fail , false );
151
+ _verifyFromIntegerFail (mapper , json );
152
+ mapper = _physMapper (EnumCoerce .class ,
153
+ CoercionInputShape .Integer , CoercionAction .Fail , false );
154
+ _verifyFromIntegerFail (mapper , json );
155
+ }
156
+
79
157
/*
80
158
/********************************************************
81
159
/* Second-level helper methods
@@ -90,16 +168,16 @@ private void _testEnumFromEmptyGlobalConfig(final CoercionInputShape shape, fina
90
168
91
169
// First, coerce to null
92
170
mapper = _globMapper (shape , CoercionAction .AsNull , allowEmpty );
93
- assertNull (_verifyFromEmptyPass (mapper , json ));
171
+ assertNull (_readEnumPass (mapper , json ));
94
172
95
173
// Then coerce as empty
96
174
mapper = _globMapper (shape , CoercionAction .AsEmpty , allowEmpty );
97
- EnumCoerce b = _verifyFromEmptyPass (mapper , json );
175
+ EnumCoerce b = _readEnumPass (mapper , json );
98
176
assertEquals (ENUM_DEFAULT , b );
99
177
100
178
// and finally, "try convert", which for Enums is same as "empty" (default)
101
179
mapper = _globMapper (shape , CoercionAction .TryConvert , allowEmpty );
102
- assertEquals (ENUM_DEFAULT , _verifyFromEmptyPass (mapper , json ));
180
+ assertEquals (ENUM_DEFAULT , _readEnumPass (mapper , json ));
103
181
}
104
182
105
183
private void _testEnumFromEmptyLogicalTypeConfig (final CoercionInputShape shape , final String json ,
@@ -111,17 +189,17 @@ private void _testEnumFromEmptyLogicalTypeConfig(final CoercionInputShape shape,
111
189
112
190
// First, coerce to null
113
191
mapper = _logMapper (LogicalType .Enum , shape , CoercionAction .AsNull , allowEmpty );
114
- b = _verifyFromEmptyPass (mapper , json );
192
+ b = _readEnumPass (mapper , json );
115
193
assertNull (b );
116
194
117
195
// Then coerce as empty
118
196
mapper = _logMapper (LogicalType .Enum , shape , CoercionAction .AsEmpty , allowEmpty );
119
- b = _verifyFromEmptyPass (mapper , json );
197
+ b = _readEnumPass (mapper , json );
120
198
assertEquals (ENUM_DEFAULT , b );
121
199
122
200
// and with TryConvert (for enums same as empty)
123
201
mapper = _logMapper (LogicalType .Enum , shape , CoercionAction .TryConvert , allowEmpty );
124
- b = _verifyFromEmptyPass (mapper , json );
202
+ b = _readEnumPass (mapper , json );
125
203
assertEquals (ENUM_DEFAULT , b );
126
204
127
205
// But also make fail again with 2-level settings
@@ -142,16 +220,16 @@ private void _testEnumFromEmptyPhysicalTypeConfig(final CoercionInputShape shape
142
220
143
221
// First, coerce to null
144
222
mapper = _physMapper (EnumCoerce .class , shape , CoercionAction .AsNull , allowEmpty );
145
- b = _verifyFromEmptyPass (mapper , json );
223
+ b = _readEnumPass (mapper , json );
146
224
assertNull (b );
147
225
148
226
// Then coerce as empty
149
227
mapper = _physMapper (EnumCoerce .class , shape , CoercionAction .AsEmpty , allowEmpty );
150
- b = _verifyFromEmptyPass (mapper , json );
228
+ b = _readEnumPass (mapper , json );
151
229
assertEquals (ENUM_DEFAULT , b );
152
230
153
231
mapper = _physMapper (EnumCoerce .class , shape , CoercionAction .TryConvert , allowEmpty );
154
- b = _verifyFromEmptyPass (mapper , json );
232
+ b = _readEnumPass (mapper , json );
155
233
assertEquals (ENUM_DEFAULT , b );
156
234
157
235
// But also make fail again with 2-level settings, with physical having precedence
@@ -201,11 +279,11 @@ private ObjectMapper _physMapper(Class<?> type, CoercionInputShape shape, Coerci
201
279
/********************************************************
202
280
*/
203
281
204
- private EnumCoerce _verifyFromEmptyPass (ObjectMapper m , String json ) throws Exception {
205
- return _verifyFromEmptyPass (m .reader (), json );
282
+ private EnumCoerce _readEnumPass (ObjectMapper m , String json ) throws Exception {
283
+ return _readEnumPass (m .reader (), json );
206
284
}
207
285
208
- private EnumCoerce _verifyFromEmptyPass (ObjectReader r , String json ) throws Exception
286
+ private EnumCoerce _readEnumPass (ObjectReader r , String json ) throws Exception
209
287
{
210
288
return r .forType (EnumCoerce .class )
211
289
.readValue (json );
@@ -217,14 +295,21 @@ private void _verifyFromEmptyFail(ObjectMapper m, String json) throws Exception
217
295
m .readValue (json , EnumCoerce .class );
218
296
fail ("Should not accept Empty/Blank String for Enum with passed settings" );
219
297
} catch (MismatchedInputException e ) {
220
- _verifyFailMessage (e );
298
+ verifyException (e , "Cannot coerce " );
299
+ verifyException (e , " empty String " , " blank String " );
300
+ assertValidLocation (e .getLocation ());
221
301
}
222
302
}
223
303
224
- private void _verifyFailMessage ( JsonProcessingException e )
304
+ private void _verifyFromIntegerFail ( ObjectMapper m , String json ) throws Exception
225
305
{
226
- verifyException (e , "Cannot coerce " );
227
- verifyException (e , " empty String " , " blank String " );
228
- assertValidLocation (e .getLocation ());
306
+ try {
307
+ m .readValue (json , EnumCoerce .class );
308
+ fail ("Should not accept Empty/Blank String for Enum with passed settings" );
309
+ } catch (MismatchedInputException e ) {
310
+ verifyException (e , "Cannot coerce Integer value (" );
311
+ verifyException (e , "but could if coercion was enabled" );
312
+ assertValidLocation (e .getLocation ());
313
+ }
229
314
}
230
315
}
0 commit comments