I am using objectMapper to deserialize xml to my object.
My xml has a section that has several possible fields, so I modeled my object using a sealed interface:
public record Root(String id, Child child) {
public sealed interface Child permits ChildA, ChildB, ChildC {
String name();
public record ChildA(String name, int a) implements Child {}
public record ChildB(String name, int a, int b) implements Child {}
public record ChildC(String name, int c) implements Child {}
}
}
I want the objectMapper to automatically try to resolve the implementation
Currently I solved the issue by using JsonCreator to manually resolve the implementation:
@JsonCreaor
static Root.Child creator(@JsonPopety("name") string name,
@JsonPopety("a") Integer a,
@JsonPopety("b") Integer b,
@JsonPopety("c") Integer c) {
if (a == null && b == null && c != null) return new Root.ChildC(name, c);
if (a != null && b != null && c == null) return new Root.ChildB(name, a, b);
if (a != null && b == null && c == null) return new Root.ChildA(name, a);
throw new IllegalStateException();
}
But this kind of manually resolution is pretty ugly, and because there are no abiguities I would hope that the mapper will be able to do it by itself, especially when I have several places like this.
The definition of my mapper is:
XmlMapper.builder()
.addModule(/*custom module for Instant class deserializer*/)
.configure(MapperFeature.ACCEPT_CASE_INSENSATIVE_PROPERTIES, true)
.build()
.setTimeZone(TimeZone.getTimeZone("UTC"));