Skip to content

Commit

Permalink
Fixed #2828 (although follow-up clean up needed, so first part)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 22, 2021
1 parent 3766578 commit 2666a7f
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 68 deletions.
2 changes: 1 addition & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Project: jackson-databind

2.13.0 (not yet released)

No changes since 2.12
#2828: Add `DatabindException` as intermediate subtype of `JsonMappingException`

2.12.1 (08-Jan-2021)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.fasterxml.jackson.databind;

import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonProcessingException;

/**
* Intermediate base class for all databind level processing problems, as
* distinct from stream-level problems or I/O issues below.
*<p>
* Added in 2.13 to eventually replace {@link com.fasterxml.jackson.databind.JsonMappingException};
* for 2.x will allow limited use as target (as catching it will also catch mapping exception)
* but will not be constructed or thrown directly.
*
* @since 2.13
*/
public abstract class DatabindException
extends JsonProcessingException
{
private static final long serialVersionUID = 3L;

protected DatabindException(String msg, JsonLocation loc, Throwable rootCause) {
super(msg, loc, rootCause);
}

protected DatabindException(String msg) {
super(msg);
}

protected DatabindException(String msg, JsonLocation loc) {
this(msg, loc, null);
}

protected DatabindException(String msg, Throwable rootCause) {
this(msg, null, rootCause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
* troubleshooting.
*/
public class JsonMappingException
extends JsonProcessingException
extends DatabindException // @since 2.13
{
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 3L;

/**
* Let's limit length of reference chain, to limit damage in cases
Expand Down
78 changes: 39 additions & 39 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,7 @@ protected void _reportUndetectableSource(Object src) throws StreamReadException
* Method called to locate deserializer for the passed root-level value.
*/
protected JsonDeserializer<Object> _findRootDeserializer(DeserializationContext ctxt)
throws JsonMappingException
throws DatabindException
{
if (_rootDeserializer != null) {
return _rootDeserializer;
Expand Down Expand Up @@ -2306,7 +2306,7 @@ protected JsonDeserializer<Object> _findRootDeserializer(DeserializationContext
* @since 2.6
*/
protected JsonDeserializer<Object> _findTreeDeserializer(DeserializationContext ctxt)
throws JsonMappingException
throws DatabindException
{
final JavaType nodeType = _jsonNodeType();
JsonDeserializer<Object> deser = _rootDeserializers.get(nodeType);
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ public void writeValue(JsonGenerator g, Object value) throws IOException
* JSON output, written to File provided.
*/
public void writeValue(File resultFile, Object value)
throws IOException, StreamWriteException, JsonMappingException
throws IOException, StreamWriteException, DatabindException
{
_writeValueAndClose(createGenerator(resultFile, JsonEncoding.UTF8), value);
}
Expand All @@ -1039,7 +1039,7 @@ public void writeValue(File resultFile, Object value)
* is closed).
*/
public void writeValue(OutputStream out, Object value)
throws IOException, StreamWriteException, JsonMappingException
throws IOException, StreamWriteException, DatabindException
{
_writeValueAndClose(createGenerator(out, JsonEncoding.UTF8), value);
}
Expand All @@ -1055,7 +1055,7 @@ public void writeValue(OutputStream out, Object value)
* is closed).
*/
public void writeValue(Writer w, Object value)
throws IOException, StreamWriteException, JsonMappingException
throws IOException, StreamWriteException, DatabindException
{
_writeValueAndClose(createGenerator(w), value);
}
Expand All @@ -1064,7 +1064,7 @@ public void writeValue(Writer w, Object value)
* @since 2.8
*/
public void writeValue(DataOutput out, Object value)
throws IOException, StreamWriteException, JsonMappingException
throws IOException, StreamWriteException, DatabindException
{
_writeValueAndClose(createGenerator(out), value);
}
Expand Down Expand Up @@ -1135,7 +1135,8 @@ public byte[] writeValueAsBytes(Object value)
*
* @since 2.2
*/
public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor) throws JsonMappingException
public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor)
throws JsonMappingException
{
_assertNotNull("type", type);
_assertNotNull("visitor", visitor);
Expand All @@ -1145,7 +1146,9 @@ public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visi
/**
* Since 2.6
*/
public void acceptJsonFormatVisitor(Class<?> type, JsonFormatVisitorWrapper visitor) throws JsonMappingException {
public void acceptJsonFormatVisitor(Class<?> type, JsonFormatVisitorWrapper visitor)
throws JsonMappingException
{
_assertNotNull("type", type);
_assertNotNull("visitor", visitor);
acceptJsonFormatVisitor(_config.constructType(type), visitor);
Expand Down Expand Up @@ -1482,7 +1485,7 @@ public Prefetch forRootType(ObjectWriter parent, JavaType newType) {
((TypeWrappedSerializer) ser).typeSerializer());
}
return new Prefetch(newType, ser, null);
} catch (JsonMappingException e) {
} catch (DatabindException e) {
// need to swallow?
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,18 @@ public void testWrapExceptions() throws Exception

try {
mapper.readValue("[{}]", new TypeReference<List<SomeObject>>() {});
} catch (JsonMappingException exc) {
} catch (DatabindException exc) {
assertEquals("I want to catch this exception", exc.getOriginalMessage());
} catch (RuntimeException exc) {
fail("The RuntimeException should have been wrapped with a JsonMappingException.");
fail("The RuntimeException should have been wrapped with a DatabindException.");
}

ObjectMapper mapperNoWrap = new ObjectMapper();
mapperNoWrap.disable(DeserializationFeature.WRAP_EXCEPTIONS);

try {
mapperNoWrap.readValue("[{}]", new TypeReference<List<SomeObject>>() {});
} catch (JsonMappingException exc) {
} catch (DatabindException exc) {
fail("It should not have wrapped the RuntimeException.");
} catch (RuntimeException exc) {
assertEquals("I want to catch this exception", exc.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testRootBeans() throws Exception
try {
bean = it.nextValue();
fail("Should not have succeeded");
} catch (JsonMappingException e) {
} catch (DatabindException e) {
verifyException(e, "Unrecognized field \"x\"");
}
// 21-May-2015, tatu: With [databind#734], recovery, we now know there's no more data!
Expand All @@ -59,7 +59,7 @@ public void testSimpleRootRecovery() throws Exception
// second one problematic
try {
it.nextValue();
} catch (JsonMappingException e) {
} catch (DatabindException e) {
verifyException(e, "Unrecognized field \"foo\"");
}

Expand Down Expand Up @@ -88,7 +88,7 @@ public void testSimpleArrayRecovery() throws Exception
// second one problematic
try {
it.nextValue();
} catch (JsonMappingException e) {
} catch (DatabindException e) {
verifyException(e, "Unrecognized field \"foo\"");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package com.fasterxml.jackson.databind.struct;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.*;

// Tests for [#81]
public class TestUnwrappedWithTypeInfo extends BaseMapTest
Expand Down Expand Up @@ -63,7 +57,7 @@ public void testDefaultUnwrappedWithTypeInfo() throws Exception
try {
mapper.writeValueAsString(outer);
fail("Expected exception to be thrown.");
} catch (JsonMappingException ex) {
} catch (DatabindException ex) {
verifyException(ex, "requires use of type information");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void testFailOnUnknownPropertyUnwrapped() throws Exception
try {
MAPPER.readValue(aposToQuotes(JSON), A.class);
fail("Exception was not thrown on unkown property");
} catch (JsonMappingException e) {
} catch (DatabindException e) {
verifyException(e, "Unrecognized field");
}
}
Expand Down

0 comments on commit 2666a7f

Please sign in to comment.