I have the below class structure.
public class Comment
{
private Integer id;
private String text;
//getters & setters
}
@Mapper(componentModel = "spring")
public interface CommentMapper
{
String map(Comment comment);
Comment map(String text);
//Comment map(String someNameHere);
}
The below is the implementation generated by mapstruct
@Override
public String map(Comment comment) {
if ( comment == null ) {
return null;
}
String string = new String();
return string;
}
@Override
public Comment map(String text) {
if ( text == null ) {
return null;
}
Comment comment = new Comment();
comment.setText( text );
return comment;
}
/*
@Override
public Comment map(String someNameHere) {
if ( someNameHere == null ) {
return null;
}
Comment comment = new Comment();
return comment;
}
*/
Question1:
The map method which takes the Comment object as parameter and returns the string is just returning an empty string object instead of setting the text property on the string and return it. Why? and how to get the text property of the comment object returned?
Question2:
when the parameter name of the map method is text it generates implementation utilizing the text property from the class or else just empty comment object. I really surprised to see that mapstruct generates different implementations depends on the parameter name too. Any explanation?
Note:
The Comment object is used as a property inside another object. There i need the above mentioned behavior. For now i managing it this way.
@Mapping(source = "entity.comment.text", target = "comment")
@Mapping(source = "dto.comment", target = "comment.text")