@JsonTypeInfo property added twice to generated JSON: add support for `JsonType.As.EXISTING_PROPERTY`
See original GitHub issueHi,
I have an abstract super class annotated with
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
where type
is an existing property of the class.
The full class hierarchy looks like this:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SubClassA.class, name = "a"),
@JsonSubTypes.Type(value = SubClassB.class, name = "b")
})
public abstract class AbstractSuperClass {
@Transient
private String type;
protected AbstractSuperClass() {
setType(getTypeValue());
}
protected abstract String getTypeValue();
// getters and setters
}
public class SubClassA extends AbstractSuperClass {
private double a;
public SubClassA(double a) {
this.a = a;
}
// getters and setters
@Override
protected String getTypeValue() {
return "a";
}
}
public class SubClassB extends AbstractSuperClass {
private int b;
public SubClassB(int b) {
this.b = b;
}
// getters and setters
@Override
protected String getTypeValue() {
return "b";
}
}
Now when a SubClass of this class gets serialised, the type
is added twice to the generated JSON. E.g. the JSON for a SubClassB would look like this:
{"type":"b","type":"b","b":42}
For the most part this does not seem to be an issue, but some clients (e.g. Postman) refuse this as Malformed JSON
.
I know I could add e.g. @JsonTypeId
or @JsonIgnore
but in that case, type
will not be included in generated JSON when using a List<AbstractSuperClass>
, which is what I definitely need.
I assume that include = JsonTypeInfo.As.EXISTING_PROPERTY
would solve my problem but this results in java.lang.IllegalStateException: Do not know how to construct standard type serializer for inclusion type: EXISTING_PROPERTY
Issue Analytics
- State:
- Created 9 years ago
- Comments:6 (3 by maintainers)
Original fix was in 2.5.0. There is one related fix (#222) that will be included in 2.6.0 (and 2.6.0-rc3 if such is released before 2.6.0 final).
Hi
Can you tell me in which version this fix has been released ? Thx Christophe