I write a lot of code these days using sealed interfaces with record subtypes so you can exhaustively pattern match on them using switch in newer Java versions.
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(value = Bar.class, name = "Bar"),
@JsonSubTypes.Type(value = Baz.class, name = "Baz")}),
sealed interface Foo {
public record Bar(Integer x) implements Foo {};
public record Baz(String y) implements Foo {};
}
I’m trying to send these over to the frontend for use in my Hilla app, and it almost works but there are a few type-generation issues - only the interface for each record is generated, without the @type property and they don’t extend the super interface.
There seems to be partial support for subtypes, used as part of the Filter mechanism, which is quite close but uses a class hierarchy with subtypes rather than a sealed interface with records - but Jackson encodes and works with them exactly the same.
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
@Type(value = OrFilter.class, name = "or"),
@Type(value = AndFilter.class, name = "and"),
@Type(value = PropertyStringFilter.class, name = "propertyString") })
public class Filter {
}
Any idea if this is supported? There’s an older post at Endpoint Generator and Class Type Generation for Derived Classes but not much more info. It’s quite a common pattern in more modern data-orientated Java apps.