Search code examples
javajbossjacksonwildflylombok

Builders on abstract classes cannot be @Jacksonized (the builder would never be used)


I have two classes OAuth2Token and CachedOAuth2Token that extends a class called AbstractOAuth2Token.

AbstractOAuth2Token.java

@SuperBuilder
@Jacksonized
@JsonSubTypes({
        @JsonSubTypes.Type(value = OAuth2Token.class),
})
@Getter
@Setter
@ToString
public abstract class AbstractOAuth2Token {
    @JsonProperty("access_token")
    private String accessToken;

    @JsonProperty("token_type")
    private String tokenType;
}

OAuth2Token.java

@Getter
@Setter
@SuperBuilder
@ToString(callSuper = true)
@JsonTypeName("OAuth2Token")
@Jacksonized
public class OAuth2Token extends AbstractOAuth2Token {
    @JsonProperty("expires_in")
    private int expiresIn;
}

CachedOAuth2Token.java

@Getter
@Setter
@SuperBuilder
@ToString(callSuper = true)
public class CachedOAuth2Token extends AbstractOAuth2Token {
    private LocalDateTime expirationDate;
}

Unfortunately my Maven project doesn't build because AbstractOAuth2Token.java: Builders on abstract classes cannot be @Jacksonized (the builder would never be used).

Even if the code works as expected if the AbstractOAuth2Token isn't abstract, then I'm able to create an instance of it using the builder which indeed isn't what I want. Its constructor is protected, so no problem there.

The idea is that I want AbstractOAuth2Token to be abstract without losing any fuctionality in the children. I'm a fan of Lombok, so I want to be able to use the autogenerated builders but together with Jackson.

It's a Wildfly 11 project with Lombok 1.18.16

How can I solve this issue?


Solution

  • Don't add @Jacksonized to your abstract base class. Non-@Jacksonized @SuperBuilders are compatible with @Jacksonized @SuperBuilders. As Jackson will never use AbstractOAuth2Token's builder directly, there is no need to configure it for Jackson explicitly.