mapstruct icon indicating copy to clipboard operation
mapstruct copied to clipboard

Support overloading for target

Open quantumlexa opened this issue 8 years ago • 6 comments

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 );
    }

}

quantumlexa avatar Mar 12 '17 17:03 quantumlexa

+1

pduartee avatar Jan 14 '20 15:01 pduartee

+1

rivierej avatar Jun 17 '20 18:06 rivierej

This was never merged? Is there a particular reason?

aioobe avatar Dec 18 '22 14:12 aioobe

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.

filiphr avatar Feb 05 '23 11:02 filiphr

Is there an estimation about when this may be merged?

onurcelik98 avatar Jun 06 '23 07:06 onurcelik98

November 2025. Bump up.

neboskreb avatar Nov 25 '25 20:11 neboskreb