Support overloading for target
Mapstruct does support overloading of target setters(see: Issue892Test test) but choosing preferred type logic is based on getter type of target - not source, which looks a bit strange. org.mapstruct.ap.internal.model.common.Type#getPropertyWriteAccessors: line 465
Please see unit test: Source:
public class Source {
private Date updatedOn;
public Source(Date updatedOn) {
this.updatedOn = updatedOn;
}
public Date getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
}
Target:
public class Target {
private long updatedOn;
public long getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn( long updatedOn ) {
this.updatedOn = updatedOn;
}
public void setUpdatedOn( Date updatedOn ) {
if (updatedOn == null) {
return;
}
this.updatedOn = updatedOn.getTime();
}
}
Mapper:
@Mapper
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mapping(target = "updatedOn", source = "updatedOn")
Target sourceToTarget( Source source );
}
UnitTest:
@WithClasses({
SourceTargetMapper.class,
Source.class,
Target.class
})
@RunWith(AnnotationProcessorTestRunner.class)
public class OverloadingTest {
@Test
public void testShouldGenerateCorrectMapperImplementation() {
Source source = new Source( new Date() );
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
Assert.assertTrue( target.getUpdatedOn() > 0 );
}
}
+1
+1
This was never merged? Is there a particular reason?
The PR for this hasn't been merged since the MapStruct team hasn't had the time to look into it in detail. The PR is not a small change (functionality wise). Therefore, we need to be careful when merging it.
Is there an estimation about when this may be merged?
November 2025. Bump up.