It seems that fasterxml treats the property of the parent object incorrectly, when used with JsonTypeInfo.As.WRAPPER_OBJECT. We have the following hierarchy:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT
)
@JsonSubTypes({
@JsonSubTypes.Type(value = FailedNotification.class, name = "FailedNotification"),
@JsonSubTypes.Type(value = StartedNotification.class, name = "StartedNotification")
})
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Notification {
@NotNull
private String code;
}
And subtypes:
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@JsonRootName("FailedNotification")
public class FailedNotification extends Notification {
private String sourceId;
}
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@JsonRootName("StartedNotification")
public class StartedNotification extends Notification {
private String sourceId;
}
But the deserialization
new XmlMapper().readValue("""
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StartedNotification>
<code>123</code>
<sourceId>source-is-here</sourceId>
</StartedNotification>
""", Notification.class);
fails with:
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'code' as a subtype of
Notification
: known type ids = [FailedNotification, Notification, StartedNotification]