Skip to content

Commit cfbbe76

Browse files
committed
Yet more cleanup post #2828; also improve exception for some bad def cases
1 parent 03f92a5 commit cfbbe76

17 files changed

+52
-41
lines changed

Diff for: src/main/java/com/fasterxml/jackson/databind/deser/DeserializerCache.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ protected JsonDeserializer<Object> _createAndCache2(DeserializationContext ctxt,
265265
} catch (IllegalArgumentException iae) {
266266
// We better only expose checked exceptions, since those
267267
// are what caller is expected to handle
268-
throw JsonMappingException.from(ctxt, ClassUtil.exceptionMessage(iae), iae);
268+
ctxt.reportBadDefinition(type, ClassUtil.exceptionMessage(iae));
269+
deser = null; // never gets here
269270
}
270271
if (deser == null) {
271272
return null;

Diff for: src/test/java/com/fasterxml/jackson/databind/convert/TestBeanConversions.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import com.fasterxml.jackson.databind.*;
1010
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1111
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
12+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
13+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
1214
import com.fasterxml.jackson.databind.node.ObjectNode;
1315
import com.fasterxml.jackson.databind.util.StdConverter;
1416

@@ -142,14 +144,14 @@ public void testErrorReporting() throws Exception
142144
// First: unknown property
143145
try {
144146
MAPPER.readValue("{\"unknownProp\":true}", BooleanBean.class);
145-
} catch (JsonMappingException e) {
147+
} catch (UnrecognizedPropertyException e) {
146148
verifyException(e, "unknownProp");
147149
}
148150

149151
// then bad conversion
150152
try {
151153
MAPPER.readValue("{\"boolProp\":\"foobar\"}", BooleanBean.class);
152-
} catch (JsonMappingException e) {
154+
} catch (InvalidFormatException e) {
153155
verifyException(e, "Cannot deserialize value of type `boolean` from String");
154156
}
155157
}

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/AnySetterTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.fasterxml.jackson.annotation.*;
66

77
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
9+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
810

911
/**
1012
* Unit tests for verifying that {@link JsonAnySetter} annotation
@@ -270,7 +272,7 @@ public void testAnySetterDisable() throws Exception
270272
MAPPER.readValue(aposToQuotes("{'value':3}"),
271273
MapImitatorDisabled.class);
272274
fail("Should not pass");
273-
} catch (JsonMappingException e) {
275+
} catch (UnrecognizedPropertyException e) {
274276
verifyException(e, "Unrecognized field \"value\"");
275277
}
276278

@@ -292,7 +294,7 @@ public void testBrokenWithDoubleAnnotations() throws Exception
292294
@SuppressWarnings("unused")
293295
Broken b = MAPPER.readValue("{ \"a\" : 3 }", Broken.class);
294296
fail("Should have gotten an exception");
295-
} catch (JsonMappingException e) {
297+
} catch (InvalidDefinitionException e) {
296298
verifyException(e, "Multiple 'any-setter' methods");
297299
}
298300
}

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public void testWrapExceptions() throws Exception
582582
.without(DeserializationFeature.WRAP_EXCEPTIONS)
583583
.readValue(quote("B"));
584584
fail("Should not pass");
585-
} catch (JsonMappingException e) {
585+
} catch (DatabindException e) {
586586
fail("Wrong exception, should not wrap, got: "+e);
587587
} catch (IllegalArgumentException e) {
588588
verifyException(e, "2164");

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKNumberDeserTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void testDeserializeDecimalProperException() throws Exception {
192192
try {
193193
MAPPER.readValue(json, MyBeanHolder.class);
194194
fail("should have raised exception");
195-
} catch (JsonMappingException e) {
195+
} catch (DatabindException e) {
196196
verifyException(e, "not numeric");
197197
}
198198
}
@@ -202,7 +202,7 @@ public void testDeserializeDecimalProperExceptionWhenIdSet() throws Exception {
202202
try {
203203
MyBeanHolder result = MAPPER.readValue(json, MyBeanHolder.class);
204204
fail("should have raised exception instead value was set to " + result.defaultValue.value.decimal.toString());
205-
} catch (JsonMappingException e) {
205+
} catch (DatabindException e) {
206206
verifyException(e, "not numeric");
207207
}
208208
}

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsDeserTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
import com.fasterxml.jackson.annotation.JsonCreator;
1212
import com.fasterxml.jackson.annotation.JsonProperty;
13+
1314
import com.fasterxml.jackson.core.*;
15+
1416
import com.fasterxml.jackson.databind.*;
1517
import com.fasterxml.jackson.databind.JsonMappingException.Reference;
1618
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
@@ -651,7 +653,7 @@ private void _testNullForPrimitiveArrays(Class<?> cls, Object defValue,
651653
try {
652654
readerNoNulls.readValue(JSON_WITH_NULL);
653655
fail("Should not pass");
654-
} catch (JsonMappingException e) {
656+
} catch (MismatchedInputException e) {
655657
verifyException(e, "Cannot coerce `null`");
656658
verifyException(e, "to element of "+SIMPLE_NAME);
657659
}
@@ -715,8 +717,8 @@ private void _testInvalidStringCoercionFail(Class<?> cls, String targetTypeName)
715717

716718
try {
717719
MAPPER.readerFor(cls).readValue(JSON);
718-
fail("Should MismatchedInputException pass");
719-
} catch (JsonMappingException e) {
720+
fail("Should not pass");
721+
} catch (MismatchedInputException e) {
720722
verifyException(e, "Cannot deserialize value of type `"+targetTypeName+"` from String \"foobar\"");
721723
}
722724
}

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/jdk/MapDeserializationTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.fasterxml.jackson.databind.*;
1313
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1414
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
15+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
16+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
1517

1618
@SuppressWarnings("serial")
1719
public class MapDeserializationTest
@@ -25,7 +27,7 @@ static class BrokenMap
2527
extends HashMap<Object,Object>
2628
{
2729
// No default ctor, nor @JsonCreators
28-
public BrokenMap(boolean dummy) { super(); }
30+
public BrokenMap(boolean dummy, boolean dummy2) { super(); }
2931
}
3032

3133
@JsonDeserialize(using=CustomMapDeserializer.class)
@@ -503,7 +505,7 @@ public void testMapError() throws Exception
503505
Object result = MAPPER.readValue("[ 1, 2 ]",
504506
new TypeReference<Map<String,String>>() { });
505507
fail("Expected an exception, but got result value: "+result);
506-
} catch (JsonMappingException jex) {
508+
} catch (MismatchedInputException jex) {
507509
verifyException(jex, "START_ARRAY");
508510
}
509511
}
@@ -514,7 +516,7 @@ public void testNoCtorMap() throws Exception
514516
BrokenMap result = MAPPER.readValue("{ \"a\" : 3 }", BrokenMap.class);
515517
// should never get here; assert added to remove compiler warning
516518
assertNull(result);
517-
} catch (JsonMappingException e) {
519+
} catch (InvalidDefinitionException e) {
518520
// instead, should get this exception:
519521
verifyException(e, "no default constructor found");
520522
}

Diff for: src/test/java/com/fasterxml/jackson/databind/exc/BasicExceptionTest.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
public class BasicExceptionTest extends BaseMapTest
1414
{
15+
static class User {
16+
public String user;
17+
}
18+
19+
static class Users {
20+
public ArrayList<User> userList;
21+
}
22+
1523
private final ObjectMapper MAPPER = newJsonMapper();
1624
private final JsonFactory JSON_F = MAPPER.getFactory();
1725

@@ -120,7 +128,7 @@ public void testLocationAddition() throws Exception
120128
try {
121129
MAPPER.readValue(problemJson, Users.class);
122130
fail("Should not pass");
123-
} catch (JsonMappingException e) { // becomes "generic" due to wrapping for passing path info
131+
} catch (DatabindException e) { // becomes "generic" due to wrapping for passing path info
124132
String msg = e.getMessage();
125133
String[] str = msg.split(" at \\[");
126134
if (str.length != 2) {
@@ -132,11 +140,4 @@ public void testLocationAddition() throws Exception
132140
assertEquals(4, loc.getColumnNr());
133141
}
134142
}
135-
static class User {
136-
public String user;
137-
}
138-
139-
static class Users {
140-
public ArrayList<User> userList;
141-
}
142143
}

Diff for: src/test/java/com/fasterxml/jackson/databind/exc/DeserExceptionTypeTest.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Unit test for verifying that exceptions are properly handled (caught,
1212
* re-thrown or wrapped, depending) with Object deserialization,
13-
* including using concrete subtypes of {@link JsonMappingException}
13+
* including using concrete subtypes of {@link DatabindException}
1414
* (and streaming-level equivalents).
1515
*/
1616
public class DeserExceptionTypeTest
@@ -56,16 +56,15 @@ public void testHandlingOfUnrecognized() throws Exception
5656

5757
/**
5858
* Simple test to check behavior when end-of-stream is encountered
59-
* without content. Used to expect EOFException (Jackson 1.x); but
60-
* nowadays ought to be JsonMappingException
59+
* without content.
6160
*/
6261
public void testExceptionWithEmpty() throws Exception
6362
{
6463
try {
6564
Object result = MAPPER.readValue(" ", Object.class);
6665
fail("Expected an exception, but got result value: "+result);
67-
} catch (Exception e) {
68-
verifyException(e, MismatchedInputException.class, "No content");
66+
} catch (MismatchedInputException e) {
67+
verifyException(e, "No content");
6968
}
7069
}
7170

@@ -112,7 +111,7 @@ public void testExceptionForNoCreators() throws Exception
112111
try {
113112
NoCreatorsBean b = MAPPER.readValue("{}", NoCreatorsBean.class);
114113
fail("Should not succeed, got: "+b);
115-
} catch (JsonMappingException e) {
114+
} catch (InvalidDefinitionException e) {
116115
verifyException(e, InvalidDefinitionException.class, "no Creators");
117116
}
118117
}

Diff for: src/test/java/com/fasterxml/jackson/databind/exc/ExceptionDeserializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void testSingleValueArrayDeserializationException() throws Exception {
157157
try {
158158
mapper.readValue(value, IOException.class);
159159
fail("Exception not thrown when attempting to deserialize an IOException wrapped in a single value array with UNWRAP_SINGLE_VALUE_ARRAYS disabled");
160-
} catch (JsonMappingException exp2) {
160+
} catch (MismatchedInputException exp2) {
161161
verifyException(exp2, "from Array value (token `JsonToken.START_ARRAY`)");
162162
}
163163
}

Diff for: src/test/java/com/fasterxml/jackson/databind/exc/ExceptionPathTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ public void testReferenceChainForInnerClass() throws Exception
3232
try {
3333
MAPPER.readValue(json, Outer.class);
3434
fail("Should not pass");
35-
} catch (JsonMappingException e) {
36-
JsonMappingException.Reference reference = e.getPath().get(0);
37-
assertEquals(getClass().getName()+"$Outer[\"inner\"]",
38-
reference.toString());
35+
} catch (ValueInstantiationException e) {
36+
String referenceStr = e.getPath().get(0).toString();
37+
assertEquals(getClass().getName()+"$Outer[\"inner\"]", referenceStr);
3938
}
4039
}
4140
}

Diff for: src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithDefaultDeserialization.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public String getQux() {
4141

4242
public void testShouldThrowExceptionWithPathReference() throws IOException {
4343
// given
44-
ObjectMapper mapper = new ObjectMapper();
44+
ObjectMapper mapper = newJsonMapper();
4545
String input = "{\"bar\":{\"baz\":{qux:\"quxValue\"))}";
4646
final String THIS = getClass().getName();
4747

Diff for: src/test/java/com/fasterxml/jackson/databind/exc/TestExceptionHandlingWithJsonCreatorDeserialization.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public String getQux() {
5252

5353
public void testShouldThrowExceptionWithPathReference() throws IOException {
5454
// given
55-
ObjectMapper mapper = new ObjectMapper();
55+
ObjectMapper mapper = newJsonMapper();
5656
String input = "{\"bar\":{\"baz\":{qux:\"quxValue\"))}";
5757
final String THIS = getClass().getName();
5858

Diff for: src/test/java/com/fasterxml/jackson/databind/introspect/TestAutoDetect.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void testVisibilityConfigOverridesForDeser() throws Exception
162162
/*Feature1347DeserBean bean =*/
163163
MAPPER.readValue(JSON, Feature1347DeserBean.class);
164164
fail("Should not pass");
165-
} catch (JsonMappingException e) {
165+
} catch (DatabindException e) { // should probably be something more specific but...
166166
verifyException(e, "Should NOT get called");
167167
}
168168

Diff for: src/test/java/com/fasterxml/jackson/databind/introspect/TestInferredMutators.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.fasterxml.jackson.databind.introspect;
22

33
import com.fasterxml.jackson.databind.BaseMapTest;
4-
import com.fasterxml.jackson.databind.JsonMappingException;
54
import com.fasterxml.jackson.databind.MapperFeature;
65
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
77

88
public class TestInferredMutators extends BaseMapTest
99
{
@@ -39,7 +39,7 @@ public void testFinalFieldIgnoral() throws Exception
3939
try {
4040
/*p =*/ mapper.readValue("{\"x\":2}", FixedPoint.class);
4141
fail("Should not try to use final field");
42-
} catch (JsonMappingException e) {
42+
} catch (UnrecognizedPropertyException e) {
4343
verifyException(e, "unrecognized field \"x\"");
4444
}
4545
}
@@ -61,7 +61,7 @@ public void testDeserializationInference() throws Exception
6161
try {
6262
p = mapper.readValue(JSON, Point.class);
6363
fail("Should not succeeed");
64-
} catch (JsonMappingException e) {
64+
} catch (UnrecognizedPropertyException e) {
6565
verifyException(e, "unrecognized field \"x\"");
6666
}
6767
}

Diff for: src/test/java/com/fasterxml/jackson/databind/jsontype/PolymorphicDeserErrorHandlingTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonTypeInfo;
55

66
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
78

89
public class PolymorphicDeserErrorHandlingTest extends BaseMapTest
910
{
@@ -58,7 +59,7 @@ public void testSubType2668() throws Exception
5859
try {
5960
/*Child1 c =*/ MAPPER.readValue(json, Child1.class); // Deserializing into Child1
6061
fail("Should not pass");
61-
} catch (JsonMappingException e) {
62+
} catch (InvalidTypeIdException e) {
6263
verifyException(e, "not subtype of");
6364
}
6465
}

Diff for: src/test/java/com/fasterxml/jackson/databind/jsontype/TestVisibleTypeId.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.fasterxml.jackson.annotation.*;
44
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
55
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
6+
67
import com.fasterxml.jackson.databind.*;
8+
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
79

810
/**
911
* Tests to verify that Type Id may be exposed during deserialization,
@@ -257,7 +259,7 @@ public void testInvalidMultipleTypeIds() throws Exception
257259
try {
258260
MAPPER.writeValueAsString(new MultipleIds());
259261
fail("Should have failed");
260-
} catch (JsonMappingException e) {
262+
} catch (InvalidDefinitionException e) {
261263
verifyException(e, "multiple type ids");
262264
}
263265
}

0 commit comments

Comments
 (0)