Nested Target in MapStruct (Using Lombok) not working fluently
Asked Answered
L

0

1

I am trying to use Mapstruct to do some nested mapping and with Lombok. As according to their latest specification mapstruct 1.2 works out of the box with Lombok.

Here is my code:

Source Classes

@Builder
@Getter
@Setter
public class PojoA {
   private String stringA;
   private int integer;
   private PojoB pojoB;
}

@Builder
@Getter
@Setter
public class PojoB {
    private long aLong;
    private String string;
    private PojoC pojoC;
}

@Builder
@Getter
@Setter
public class PojoC {
    private int value;
}

Target Classes

@Setter
@Getter
@ToString
public class Target {
    private String targetString;
    private int integer;
    private TargetPrime targetPrime;
}

@ToString
@Getter
@Setter
public class TargetPrime {
    private long aLong;
    private String string;
    private TargetPrimePrime targetPrimePrime;
}

@ToString
@Getter
@Setter
public class TargetPrimePrime {
    private int value;
}

Mapper

@Mapper
public interface PojoMapper {

    PojoMapper INSTANCE = Mappers.getMapper(PojoMapper.class);

    @Mapping(source = "stringA", target = "targetString")
    @Mapping(source = "pojoB", target = "targetPrime")
    Target pojoAToTarget(PojoA pojoA);

    TargetPrimePrime map(PojoC pojoC);

    @Mapping(source = "pojoC", target = "targetPrimePrime")
    TargetPrime mapToPrime(PojoB pojoB);
}

As it is written that MapStruct will now work with Lombok out of the box, this means that the mapping code should work. But instead, it gives me a compilation error "that it could not find the setter setTargetPrime".

But surprisingly if I add getter/setter only for targetPrime and keep rest as it is then it works fine with Lombok.

For eg.

Updated Target

@Setter
@Getter
@ToString
public class Target {
    private String targetString;
    private int integer;
    private TargetPrime targetPrime;

    public TargetPrime getTargetPrime() {
        return targetPrime;
    }

    public void setTargetPrime(TargetPrime targetPrime) {
        this.targetPrime = targetPrime;
    }
}

And run this afterward

PojoC pojoC = PojoC.builder().value(12345).build();
        PojoB pojoB = PojoB.builder().aLong(15).string("AutoScanned").pojoC(pojoC).build();
        PojoA pojoA = PojoA.builder().integer(10).stringA("Testing").pojoB(pojoB).build();
        Target target = PojoMapper.INSTANCE.pojoAToTarget(pojoA);
        System.out.println(target);

and I will get successful output with all the mappings.

OUTPUT

Target(targetString=Testing, integer=10, targetPrime=TargetPrime(aLong=15, string=AutoScanned, targetPrimePrime=TargetPrimePrime(value=12345)))

So, As I can't remove Lombok from all the projects that I am working on because my team likes Lombok a lot so, Does Lombok really works out of the box for mapping (mutable objects) and if yes, what am I doing wrong in the above code?

Landre answered 12/2, 2018 at 18:54 Comment(3)
Without manually adding a setter I get the expected output. What does your POM look like and how are you building the test?Cladoceran
Additional question which versions of MapStruct and Lombok are you using? It is strange something to work and something not to work.Rattlehead
Got it figured out, had to clean build everything and now it works.Landre

© 2022 - 2024 — McMap. All rights reserved.