Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Coercion fail message around Array and Object #3962

Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,7 @@ protected final String _parseString(JsonParser p, DeserializationContext ctxt,
return ob.toString();
// 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
case JsonTokenId.ID_START_OBJECT:
return ctxt.extractScalarFromObject(p, this, _valueClass);
return ctxt.extractScalarFromObject(p, this, rawTargetType);
case JsonTokenId.ID_NUMBER_INT:
act = _checkIntToStringCoercion(p, ctxt, rawTargetType);
break;
Expand Down Expand Up @@ -1453,7 +1453,7 @@ protected final String _parseString(JsonParser p, DeserializationContext ctxt,
return text;
}
}
return (String) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
return (String) ctxt.handleUnexpectedToken(rawTargetType, p);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.fasterxml.jackson.databind.convert;

import java.util.List;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.util.List;

public class DisableCoercions3690Test extends BaseMapTest
{
Expand All @@ -14,6 +17,18 @@ static class Input3690 {
public List<String> field;
}

static class Input3924<T> {
private T field;

public T getField() {
return field;
}

public void setField(T field) {
this.field = field;
}
}

// [databind#3690]
public void testCoercionFail3690() throws Exception
{
Expand All @@ -38,4 +53,50 @@ public void testCoercionFail3690() throws Exception
verifyException(e, "to `java.lang.String` value");
}
}

// [databind#3924]
public void testFailMessage3924() throws Exception {
// Arrange : Building a strict ObjectMapper.
ObjectMapper mapper = jsonMapperBuilder()
.withCoercionConfigDefaults(config -> {
config.setCoercion(CoercionInputShape.Boolean, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Float, CoercionAction.Fail)
.setCoercion(CoercionInputShape.String, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Array, CoercionAction.Fail)
.setCoercion(CoercionInputShape.Object, CoercionAction.Fail);
})
.build();

// Arrange : Type configuration
TypeFactory typeFactory = mapper.getTypeFactory();
JavaType arrayType = typeFactory.constructParametricType(List.class, String.class);
JavaType inputType = typeFactory.constructParametricType(Input3924.class, arrayType);

// Act & Assert
_verifyFailedCoercionWithInvalidFormat("{ \"field\": [ 1 ] }",
"Cannot coerce Integer value (1) to `java.lang.String` value",
mapper, inputType);

_verifyFailedCoercionWithInvalidFormat("{ \"field\": [ [ 1 ] ] }",
"Cannot deserialize value of type `java.lang.String` from Array value",
mapper, inputType);

_verifyFailedCoercionWithInvalidFormat("{ \"field\": [ { \"field\": 1 } ] }",
"Cannot deserialize value of type `java.lang.String` from Object value",
mapper, inputType);

}

private void _verifyFailedCoercionWithInvalidFormat(String jsonStr, String expectedMsg, ObjectMapper mapper,
JavaType inputType) throws Exception
{
try {
mapper.readValue(jsonStr, inputType);
fail("Should not pass");
} catch (MismatchedInputException e) {
assertEquals(String.class, e.getTargetType());
JooHyukKim marked this conversation as resolved.
Show resolved Hide resolved
verifyException(e, expectedMsg);
}
}
}