I am using mapstruct and I am wondering if there are any ways to set null value for some target properties. For example,
public class MySource {
private String prop1;
private String prop2;
public MySource() {
// Initialization.
}
// Getters - Setters.
}
public class MySourceDto {
private String prop1;
private String prop2;
public MySourceDto() {
// Initialization.
}
// Getters - Setters.
}
@Mapper
public interface MySourceMapper {
@Mappings({
@Mapping(target = "prop1", propertyToSetNull = null)})
public MySourceDto toView(MySource mySource);
}
I would love the above source, dto and mapper to generate the below source code,
@Component
public class MySourceMapperImpl implements MySourceMapper {
@Override
public MySourceDto toView(MySource mySource) {
if ( mySource == null ) {
return null;
}
MySourceDto mySourceDto = new MySourceDto();
mySourceDto.setProp1( mySource.getId() );
mySourceDto.setProp2( null );
return mySourceDto;
}
}