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

Handle DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES correctly #38

Merged
merged 1 commit into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ protected final boolean _deserializeBoolean(JsonParser p, DeserializationContext
JsonToken t = p.getCurrentToken();
if (t == JsonToken.VALUE_TRUE) return true;
if (t == JsonToken.VALUE_FALSE) return false;
if (t == JsonToken.VALUE_NULL) return false;
if (t == JsonToken.VALUE_NULL) {
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
_failNullToPrimitiveCoercion(ctxt, "boolean");
} else {
return false;
}
}

if (t == JsonToken.VALUE_NUMBER_INT) {
// 11-Jan-2012, tatus: May be outside of int...
Expand Down Expand Up @@ -264,7 +270,11 @@ protected final int _deserializeInt(JsonParser p, DeserializationContext ctxt)
return p.getValueAsInt();
}
if (t == JsonToken.VALUE_NULL) {
return 0;
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
_failNullToPrimitiveCoercion(ctxt, "int");
} else {
return 0;
}
}
if (t == JsonToken.START_ARRAY && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
p.nextToken();
Expand Down Expand Up @@ -300,7 +310,11 @@ protected final long _deserializeLong(JsonParser p, DeserializationContext ctxt)
} catch (IllegalArgumentException iae) { }
throw ctxt.weirdStringException(text, Long.TYPE, "not a valid long value");
case JsonTokenId.ID_NULL:
return 0L;
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
_failNullToPrimitiveCoercion(ctxt, "long");
} else {
return 0L;
}
case JsonTokenId.ID_START_ARRAY:
if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
p.nextToken();
Expand Down Expand Up @@ -368,7 +382,14 @@ protected final boolean _deserializeBooleanFromOther(JsonParser p, Deserializati
}

// // More helper methods from StdDeserializer


protected void _failNullToPrimitiveCoercion(DeserializationContext ctxt, String type) throws JsonMappingException
{
ctxt.reportInputMismatch(getType(),
"Cannot map `null` into type %s (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)",
type);
}

protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctxt,
String type) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.fasterxml.jackson.module.afterburner.deser;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.module.afterburner.AfterburnerTestBase;

public class TestFailOnPrimitiveFromNullDeserialization extends AfterburnerTestBase
{
static class LongBean
{
public long value;
}

static class IntBean
{
public int value;
}

static class BooleanBean
{
public boolean value;
}

static class DoubleBean
{
public double value;
}

private final static String BEAN_WITH_NULL_VALUE = "{\"value\": null}";

private final ObjectMapper MAPPER = newObjectMapper();
private final ObjectMapper FAIL_ON_NULL_MAPPER = newObjectMapper()
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);

public void testPassPrimitiveFromNull() throws Exception
{
LongBean longBean = MAPPER.readValue(BEAN_WITH_NULL_VALUE, LongBean.class);
IntBean intBean = MAPPER.readValue(BEAN_WITH_NULL_VALUE, IntBean.class);
BooleanBean booleanBean = MAPPER.readValue(BEAN_WITH_NULL_VALUE, BooleanBean.class);
DoubleBean doubleBean = MAPPER.readValue(BEAN_WITH_NULL_VALUE, DoubleBean.class);
assertEquals(longBean.value, 0);
assertEquals(intBean.value, 0);
assertEquals(booleanBean.value, false);
assertEquals(doubleBean.value, 0.0);
}

public void testFailPrimitiveFromNull() throws Exception
{
try {
FAIL_ON_NULL_MAPPER.readValue(BEAN_WITH_NULL_VALUE, IntBean.class);
fail();
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type int");
}
try {
FAIL_ON_NULL_MAPPER.readValue(BEAN_WITH_NULL_VALUE, LongBean.class);
fail();
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type long");
}
try {
FAIL_ON_NULL_MAPPER.readValue(BEAN_WITH_NULL_VALUE, BooleanBean.class);
fail();
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type boolean");
}
try {
FAIL_ON_NULL_MAPPER.readValue(BEAN_WITH_NULL_VALUE, DoubleBean.class);
fail();
} catch (MismatchedInputException e) {
verifyException(e, "Cannot map `null` into type double");
}
}
}