|
11 | 11 | import com.fasterxml.jackson.databind.*;
|
12 | 12 | import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
13 | 13 |
|
14 |
| -// for [databind#1106] |
15 | 14 | public class ScalarCoercionTest extends BaseMapTest
|
16 | 15 | {
|
| 16 | + static class BooleanPOJO { |
| 17 | + public boolean value; |
| 18 | + } |
| 19 | + |
17 | 20 | private final ObjectMapper COERCING_MAPPER = jsonMapperBuilder()
|
18 | 21 | .enable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
|
19 | 22 | .build();
|
@@ -161,11 +164,25 @@ public void testStringCoercionFail() throws Exception
|
161 | 164 | _verifyRootStringCoerceFail("123.0", BigDecimal.class);
|
162 | 165 | }
|
163 | 166 |
|
| 167 | + public void testToBooleanCoercionFailBytes() throws Exception |
| 168 | + { |
| 169 | + final String beanDoc = aposToQuotes("{'value':1}"); |
| 170 | + _verifyBooleanCoerceFail("1", true, JsonToken.VALUE_NUMBER_INT, "1", Boolean.TYPE); |
| 171 | + _verifyBooleanCoerceFail("1", true, JsonToken.VALUE_NUMBER_INT, "1", Boolean.class); |
| 172 | + _verifyBooleanCoerceFail(beanDoc, true, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class); |
| 173 | + } |
| 174 | + |
| 175 | + public void testToBooleanCoercionFailChars() throws Exception |
| 176 | + { |
| 177 | + final String beanDoc = aposToQuotes("{'value':1}"); |
| 178 | + _verifyBooleanCoerceFail("1", false, JsonToken.VALUE_NUMBER_INT, "1", Boolean.TYPE); |
| 179 | + _verifyBooleanCoerceFail("1", false, JsonToken.VALUE_NUMBER_INT, "1", Boolean.class); |
| 180 | + _verifyBooleanCoerceFail(beanDoc, false, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class); |
| 181 | + } |
| 182 | + |
164 | 183 | public void testMiscCoercionFail() throws Exception
|
165 | 184 | {
|
166 | 185 | // And then we have coercions from more esoteric types too
|
167 |
| - _verifyCoerceFail("1", Boolean.TYPE); |
168 |
| - _verifyCoerceFail("1", Boolean.class); |
169 | 186 |
|
170 | 187 | _verifyCoerceFail("65", Character.class);
|
171 | 188 | _verifyCoerceFail("65", Character.TYPE);
|
@@ -230,4 +247,46 @@ private void _verifyStringCoerceFail(JsonParser p,
|
230 | 247 | assertEquals(unquotedValue, p.getText());
|
231 | 248 | }
|
232 | 249 | }
|
| 250 | + |
| 251 | + private void _verifyBooleanCoerceFail(String doc, boolean useBytes, |
| 252 | + JsonToken tokenType, String tokenValue, Class<?> targetType) throws IOException |
| 253 | + { |
| 254 | + // Test failure for root value: for both byte- and char-backed sources. |
| 255 | + |
| 256 | + // [databind#2635]: important, need to use `readValue()` that takes content and NOT |
| 257 | + // JsonParser, as this forces closing of underlying parser and exposes more issues. |
| 258 | + |
| 259 | + final ObjectReader r = NOT_COERCING_MAPPER.readerFor(targetType); |
| 260 | + try { |
| 261 | + if (useBytes) { |
| 262 | + r.readValue(utf8Bytes(doc)); |
| 263 | + } else { |
| 264 | + r.readValue(doc); |
| 265 | + } |
| 266 | + fail("Should not have allowed coercion"); |
| 267 | + } catch (MismatchedInputException e) { |
| 268 | + _verifyBooleanCoerceFailReason(e, tokenType, tokenValue); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + @SuppressWarnings("resource") |
| 273 | + private void _verifyBooleanCoerceFailReason(MismatchedInputException e, |
| 274 | + JsonToken tokenType, String tokenValue) throws IOException |
| 275 | + { |
| 276 | + verifyException(e, "Cannot coerce "); |
| 277 | + verifyException(e, " for type `"); |
| 278 | + verifyException(e, "enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow"); |
| 279 | + |
| 280 | + JsonParser p = (JsonParser) e.getProcessor(); |
| 281 | + |
| 282 | + assertToken(tokenType, p.currentToken()); |
| 283 | + |
| 284 | + final String text = p.getText(); |
| 285 | + |
| 286 | + if (!tokenValue.equals(text)) { |
| 287 | + String textDesc = (text == null) ? "NULL" : quote(text); |
| 288 | + fail("Token text ("+textDesc+") via parser of type "+p.getClass().getName() |
| 289 | + +" not as expected ("+quote(tokenValue)+")"); |
| 290 | + } |
| 291 | + } |
233 | 292 | }
|
0 commit comments