Skip to content

In-compatible type exception when build method return super type #763

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

Merged
merged 1 commit into from
Apr 21, 2015
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 @@ -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);
}
}

}