Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map from multiple sources DTOs to one target

Tags:

java

mapstruct

Using MapStruct. Take as example:

class Dto {
    DtoA a;
    DtoB b;
}

class DtoA {
    Long id;
    //...
}

class DtoB {
    Long id;
    //...
}


class Entity {
    AB ab;
}

How do I map DtoA and DtoB to AB ?

I tried:

public abstract Entity toEntity(Dto dto);

@Mappings({
            @Mapping(source = "a", target = "ab.a"),
            @Mapping(source = "b", target = "ab.b")
)}
public abstract AB toABEntity(DtoA a, DtoB b);

Altough code has been generated*, the method toABEntity is not invoked.

*Badly, as it sets first a but then to set b creates a new instance of ab, so a is lost.

like image 687
anat0lius Avatar asked Nov 05 '18 11:11

anat0lius


1 Answers

As far as I understand You want to map Entity to Dto and combine two fields Dto.a and Dto.b to a single field Entity.ab.

Normally when you try to do this like this:

@Mapper
public interface TestMapper {
    @Mappings({
        @Mapping(source = "a.id", target = "ab.aId", qualifiedByName = "toAB"),
        @Mapping(source = "b.id", target = "ab.bId", qualifiedByName = "toAB"),
    })
    Entity toEntity(Dto dto);
}

The generated mapper overides ab instance for every @Mapping having a target property within ab. It's clearly a bug and there's a ticket for this at MapStructs GitHub: https://github.com/mapstruct/mapstruct/issues/1148

There's a workaround though:

@Mapper
public interface TestMapper {
    @Mappings({
        @Mapping(source = "dto", target = "ab", qualifiedByName = "toAB"),
    })
    Entity toEntity(Dto dto);

    @Mappings({
        @Mapping(target = "aId", source = "a.id"),
        @Mapping(target = "bId", source = "b.id"),
    })
    AB toAB(Dto dto);
}

I assumed the AB class to be:

class AB {
    public Long aId;
    public Long bId;
}

Generated mapper code:

public class TestMapperImpl implements TestMapper {

    @Override
    public Entity toEntity(Dto dto) {
        if ( dto == null ) {
            return null;
        }

        Entity entity = new Entity();

        entity.ab = toAB( dto );

        return entity;
    }

    @Override
    public AB toAB(Dto dto) {
        if ( dto == null ) {
            return null;
        }

        AB aB = new AB();

        Long id = dtoBId( dto );
        if ( id != null ) {
            aB.bId = id;
        }
        Long id1 = dtoAId( dto );
        if ( id1 != null ) {
            aB.aId = id1;
        }

        return aB;
    }

    private Long dtoBId(Dto dto) {
        if ( dto == null ) {
            return null;
        }
        DtoB b = dto.b;
        if ( b == null ) {
            return null;
        }
        Long id = b.id;
        if ( id == null ) {
            return null;
        }
        return id;
    }

    private Long dtoAId(Dto dto) {
        if ( dto == null ) {
            return null;
        }
        DtoA a = dto.a;
        if ( a == null ) {
            return null;
        }
        Long id = a.id;
        if ( id == null ) {
            return null;
        }
        return id;
    }
}
like image 123
jannis Avatar answered Nov 06 '22 17:11

jannis