Conditional Mapping

91 views
Skip to first unread message

Durgadas Kamath

unread,
Jan 23, 2020, 6:32:20 AM1/23/20
to mapstruct-users

Is it possible to map fields only if certain expression is met ? What are the approaches ?

For e.g
Let's say I have XYZ, XYZ and XYZMapper. Along side , there is a "SecurityUtils.isAdmin()" method that provides boolean result to indicate whether it's admin or not. If its admin, i want Object b to be mapped else I want that value to be null.

@Getter
@Setter
public class XYZ{
 
private String a;
 
private Object b;
}


@Getter
@Setter
public class XYZDto{
 
private String a;
 
private Object b;
}

@Mapper(component="spring")
public interface XYZMapper{
     XYZDto toXYZDto(XYZ xyz);
     XYZ toXYZ(XYZDto xyzDto);
}

SecurityUtils.isAdmin() --> true of false


Reply all
Reply to author
Forward
0 new messages