gwt-jackson icon indicating copy to clipboard operation
gwt-jackson copied to clipboard

JsonTypeInfo defaultImpl attribute not working

Open rvervaek opened this issue 6 years ago • 1 comments

Hi,

I'm having issues using the 'defaultImpl' attribute of JsonTypeInfo annotation. This is the situation:

` public class Example {

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class", defaultImpl = Example.class) public class ExampleExtended extends Example {

private boolean isImport;

public boolean isImport() {
    return isImport;
}

public void setImport(boolean anImport) {
    isImport = anImport;
}

}

public class JsonConfig extends AbstractConfiguration {

@Override
protected void configure() {
    addMixInAnnotations(Example.class, ExampleExtended.class);
}

} `

My client application makes HTTP calls to 2 different servers. One server returns ExampleExtended instances and thus adds the '@class' property containing the type information. The second server returns Example instances and thus does not add the '@class' property containing the type information. However I specified the attribute 'defaultImpl' that will use that Example class when the type information is not present as explained in the javadoc:

Optional property that can be used to specify default implementation class to use for deserialization if type identifier is either not present, or can not be mapped to a registered type (which can occur for ids, but not when specifying explicit class to use).

But when receiving the Example object, a JsonDeserializationException is thrown with the following message:

Cannot find the property @class containing the type information

I tested this also with 'JsonTypeInfo.Id.NAME' but this gives the same error:

Cannot find the property @type containing the type information

I'm using the following versions:

  • gwt-jackson: 0.15.4
  • jackson-annotations: 2.9.0
  • gwt: 2.8.2

rvervaek avatar Feb 28 '19 10:02 rvervaek

I see I have the same problem.

I have a class DataDto public class DataDto { List<A> data; // + getters and setters }

The class A (default class) public class A { String id; // + getters and setters }

The class B (extends A) @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class", defaultImpl = A.class) public class B extends A { String name; // + getters and setters }

From 1 server, I get an A json. It's deserialized as A { data: [ { id = 5 }, {id = 6} ] }

From another server, I get an B json. It's deserialized as A but should be as B. { data: [ { @class: "B", id = 5, name = "test1" }, { @class: "B", id = 6, name = "test2"} ] }

Any clues?

I tried to add all possible combinations in whitelist without luck. Example whitelist("x.x.x.B");

jorissynaeve avatar Mar 23 '21 16:03 jorissynaeve