I want the generated mapstruct mapping method to return null if all the properties referenced in @Mapping/source are null. For exemple, I have the following mapping :
@Mappings({
@Mapping(target = "id", source = "tagRecord.tagId"),
@Mapping(target = "label", source = "tagRecord.tagLabel")
})
Tag mapToBean(TagRecord tagRecord);
The generated method is then :
public Tag mapToBean(TagRecord tagRecord) {
if ( tagRecord == null ) {
return null;
}
Tag tag_ = new Tag();
if ( tagRecord.getTagId() != null ) {
tag_.setId( tagRecord.getTagId() );
}
if ( tagRecord.getTagLabel() != null ) {
tag_.setLabel( tagRecord.getTagLabel() );
}
return tag_;
}
Test case : TagRecord object is not null but has tagId==null and tagLibelle==null.
Current behaviour : the returned Tag object is not null, but has tagId==null and tagLibelle==null
What I actually want the generated method to do is return a null Tag object if (tagRecord.getTagId() == null && tagRecord.getTagLabel() == null). Is it possible and how can I achieve this?