Skip to content

Commit

Permalink
In-compatible type exception when build method return super type
Browse files Browse the repository at this point in the history
  • Loading branch information
agavrilov76 committed Apr 20, 2015
1 parent 43a4a3f commit e1159f8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public JsonDeserializer<?> buildBuilderBased(JavaType valueType,
}
// also: type of the method must be compatible
Class<?> rawBuildType = _buildMethod.getRawReturnType();
if (!valueType.getRawClass().isAssignableFrom(rawBuildType)) {
if (!rawBuildType.isAssignableFrom(valueType.getRawClass())) {
throw new IllegalArgumentException("Build method '"+_buildMethod.getFullName()
+" has bad return type ("+rawBuildType.getName()
+"), not compatible with POJO type ("+valueType.getRawClass().getName()+")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
Expand Down Expand Up @@ -157,7 +158,55 @@ public CreatorValue build() {
return new CreatorValue(a, b, c);
}
}


@JsonDeserialize(builder=ValueInterfaceBuilder.class)
interface ValueInterface {
int getX();
}

static class ValueInterfaceImpl implements ValueInterface
{
final int _x;

protected ValueInterfaceImpl(int x) {
_x = x+1;
}

@Override
public int getX() {
return _x;
}
}
static class ValueInterfaceBuilder
{
public int x;

public ValueInterfaceBuilder withX(int x0) {
this.x = x0;
return this;
}

public ValueInterface build() {
return new ValueInterfaceImpl(x);
}
}
@JsonDeserialize(builder = ValueBuilderWrongBuildType.class)
static class ValueClassWrongBuildType {

}
static class ValueBuilderWrongBuildType
{
public int x;

public ValueBuilderWrongBuildType withX(int x0) {
this.x = x0;
return this;
}

public ValueClassXY build() {
return null;
}
}
/*
/**********************************************************
/* Unit tests
Expand Down Expand Up @@ -214,4 +263,25 @@ public void testWithCreator() throws Exception
assertEquals(2, value.b);
assertEquals(3, value.c);
}

public void testBuilderMethodReturnInterface() throws Exception
{
final String json = "{\"x\":1}";
ValueInterface value = mapper.readValue(json, ValueInterface.class);
assertEquals(2, value.getX());
}

public void testBuilderMethodReturnInvalidType() throws Exception
{
final String json = "{\"x\":1}";
try {
mapper.readValue(json, ValueClassWrongBuildType.class);
fail("Missing expected JsonProcessingException exception");
} catch(JsonProcessingException e) {
assertTrue(
"Exception cause must be IllegalArgumentException",
e.getCause() instanceof IllegalArgumentException);
}
}

}

0 comments on commit e1159f8

Please sign in to comment.