Skip to content

Support custom source presence checker expressions for mappings and custom method mappings #2084

Description

@ivangsa

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions