Skip to content

Commit

Permalink
Fixed #2973
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 9, 2020
1 parent b66dfb6 commit 7fd0af3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project: jackson-databind
#2962: Auto-detection of constructor-based creator method skipped if there is
an annotated factory-based creator method (regression from 2.11)
(reported by Halil I-S)
#2973: DeserializationProblemHandler is not invoked when trying to deserializing String
(reported by zigzago@github)

2.12.0 (29-Nov-2020)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,9 +869,7 @@ public String extractScalarFromObject(JsonParser p, JsonDeserializer<?> deser,
Class<?> scalarType)
throws IOException
{
return reportInputMismatch(scalarType, String.format(
"Cannot deserialize value of type %s from %s (token `JsonToken.START_OBJECT`)",
ClassUtil.getClassDescription(scalarType), _shapeForToken(JsonToken.START_OBJECT)));
return (String) handleUnexpectedToken(scalarType, p);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ public void testPrimitivePropertyWithHandler() throws Exception {
assertNotNull(result);
assertEquals(1, result.a);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.fasterxml.jackson.databind.deser.filter;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;

public class ProblemHandler2973Test extends BaseMapTest
{
// [databind#2973]
static class WeirdTokenHandler
extends DeserializationProblemHandler
{
@Override
public Object handleUnexpectedToken(DeserializationContext ctxt,
JavaType targetType, JsonToken t, JsonParser p,
String failureMsg)
throws IOException
{
String result = p.currentToken().toString();
p.skipChildren();
return result;
}
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

// [databind#2973]
public void testUnexpectedToken2973() throws Exception
{
// First: without handler, should get certain failure
ObjectMapper mapper = sharedMapper();
try {
mapper.readValue("{ }", String.class);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize value of type `java.lang.String` from Object value");
}

// But DeserializationProblemHandler should resolve:
mapper = jsonMapperBuilder()
.addHandler(new WeirdTokenHandler())
.build();
;
String str = mapper.readValue("{ }", String.class);
assertEquals("START_OBJECT", str);
}
}

0 comments on commit 7fd0af3

Please sign in to comment.