Mapstruct map nested from multiple source

23 views Asked by At

I have a Y inside X and a ZDTO inside YDTO but there is no Z inside Y. I want to map X to XDTO with a method like XDTO xToXDTO(X x, Z z)

Given X,Y,Z as follows:

public class X {
    private Y y;
    private Integer xa;
    private Integer xb;
}
public class Y {
    private Integer ya;
    private Integer yb;
}
public class Z {
    private Integer za;
    private Integer zb;
}

And their DTOs as follows:

public class XDTO {
    private YDTO y;
    private Integer xa;
    private Integer xb;
}
public class YDTO {
    private ZDTO z;
    private Integer ya;
    private Integer yb;
}
public class ZDTO {
    private Integer za;
    private Integer zb;
}

I have this Mapper class:

@Mapper
public interface XYZMapper {

    XDTO xToXDTO(X x, Z z);
    YDTO yToYDTO(Y y, Z z);
    ZDTO zToZDTO(Z z);
}

I expect that the mapper calls yToYDTO for mapping Y to YDTO, but when I see the generated code I see it has created a yToYDTO1 and is not using the YDTO yToYDTO(Y y, Z z)

This is the generated code by MapStruct

public XDTO xToXDTO(X x, Z z) {
    if ( x == null && z == null ) {
        return null;
    }

    XDTO xDTO = new XDTO();

    if ( x != null ) {
        xDTO.setY( yToYDTO1( x.getY() ) );
        xDTO.setXa( x.getXa() );
        xDTO.setXb( x.getXb() );
    }

    return xDTO;
}
0

There are 0 answers