I came across a situation today where the Mapstruct was not picking the java expression conversion defined in the original mapper class, while converting a different type which uses the original class.
So I have:
Type1 Mapper which has:
@Mapping(target="code", expression = "java("type1-" + type1Entity.getId())")
Type1Model toType1Model(type1Entity);
this works fine while converting Type 1 entity to model objects
Also I have Type2, which has
class Type2 {
/*other instance variables*/
Type1 type1;
}
and I have Type2Mapper as:
@Mapping(target="code", expression = "java("type2-" + type2Entity.getId())")
Type2Model toType2Model(type2Entity);
this converts type2 entity to model perfectly using the java expression, but the inner type1 variable is converted without the @Mapping defined in Type1 mapper. So the code field inside the converted entity is not present for Type1. All other fields are present.
I checked the generated mapperImpl file for Type2 and the corresponding method there for converting type1 doesn’t contains the setter for the code field.
Why is this happening? Why is mapper using the expression of Type1 for converting Type1 standalone, but not using same expression while converting Type1 inside Type2.
Note: If I copy the conversion declaration for Type1 explicitly in Type2 mapper class, it seems to work. Although I am not sure yet what ambiguous impact it might have.