Reputation: 33
I have a simple class that uses a builder pattern.
public class Foo implements FooInterface {
.....
public static final class Builder {
public Builder setValueA(String value) {...}
public Builder setValueB(String value) {...}
public FooInterface build() {...}
}
}
Note that my Builder.build() method actually returns an object of the interface type and not the implemented type. I then mixin annotations (because the class Foo is actually a 3rd party class) for defining the sub types of the interface FooInterface like so:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Foo.class, name = "foo")})
public abstract class FooInterfaceMixin {}
And mixin for defining the builder pattern of class Foo, like so:
@JsonTypeName("foo")
@JsonDeserialize(builder=Foo.Builder.class)
public abstract class FooMixin {}
Finally, to deserialize, I do:
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(FooInterface.class, FooInterfaceMixin.class);
mapper.addMixIn(Foo.class, FooMixin.class);
final String jsonString = "{\"type\":\"foo\", \"valueA\":\"a\", \"valueB\":\"b\"}";
FooInterface foo = mapper.readValue(jsonString, FooInterface.class);
I get the error Build method 'Foo$Builder#build(0 params) has bad return type (FooInterface), not compatible with POJO type (Foo)
It looks like this is the culprit, but I don't understand why returning a super type in a build method wont be acceptable. Any ideas?
Upvotes: 3
Views: 3193
Reputation: 10853
It looks like a Jackson bug. I've reported an issue: https://github.com/FasterXML/jackson-databind/issues/761
Upvotes: 2