Currently mapstruct only allows presence checker methods on the owning source object.
Some types (like java.util.Options, guava Optional, JsonNullable, ...) have presence checker method on the property itself.
With a custom presence checker expression configuration we could write a mapper like:
@Mapper
public interface OptionalMapper {
@SourcePresenceCheckerConfig(
expression = "java( <SOURCE>.<READ_ACCESSOR>() != null && <SOURCE>.<READ_ACCESSOR>().isPresent() )")
default <T> T fromOptional(final Optional<T> optional) {
return optional.orElse(null);
}
// nulls are handled inside method
@SourcePresenceCheckerConfig(nullValueCheckStrategy = NullValueCheckStrategy.NEVER)
default <T> Optional<T> asOptional(final T optional) {
return optional != null ? Optional.of(optional) : Optional.empty();
}
}
or a mapping like:
@Mapping(target = "prop1", source ="prop1", sourcePresenceCheckerExpression = "java( source != null && source.getProp1().isPresent() )")
TargetWithPlainValues asPlain(SourceWithOptional source);
This may address this question:
https://stackoverflow.com/questions/61371019/custom-source-presence-checking-method-name-in-mapstruct
and this issue in a general manner:
#674
Currently mapstruct only allows presence checker methods on the owning source object.
Some types (like java.util.Options, guava Optional, JsonNullable, ...) have presence checker method on the property itself.
With a custom presence checker expression configuration we could write a mapper like:
or a mapping like:
This may address this question:
https://stackoverflow.com/questions/61371019/custom-source-presence-checking-method-name-in-mapstruct
and this issue in a general manner:
#674