10

I have the following classes and mapper to map them. How can I configure Mapstruct to "not" use the Lombok builder? (without removing the @Builder annotation)? When using the latest version of Lombok and mapstruct, mapstruct will automatically use the Builder when the @Builder annotation is used. I can not find a way to disable that, as I need the Instance in an @AfterMapping method as the builder doesn't expose all required methods (@SuperBuilder is not allowed in this use case)

@Entity(name = "user_details")
@Data
@Builder
public class User extends AuditableEntityBase {

    @Version
    @NotNull
    private Integer version;

    @NotNull
    private String name;

    @NotNull
    private String email;

    @NotNull
    private Address address; // Just another Class containing another class that is mapped as well.

}

@Value
@Builder
public class UserDto extends AuditableEntityBaseDto {

    @NotNull
    private Integer version;

    @NotNull
    private String name;

    @NotNull
    private String email;


    @NotNull
    private AddressDto address;
}


@Mapper(componentModel = "spring")
class UserRestMapper {
    public abstract UserDto map(User obj);

}

    @AfterMapping
    public void decorate(User source, @MappingTarget AuditableEntityBase target) {
// Method is never called.
// Method is called in case  the second argument is: "@MappingTarget UserDto.UserDtoBuilder target"
    }
edbras
  • 4,145
  • 9
  • 41
  • 78

2 Answers2

16

If you want to disable using builders you can do so by adding @Builder(disableBuilder = true) to your @Mapper.

e.g.

@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
class UserRestMapper {
    public abstract UserDto map(User obj);

}

Note @Builder is from the org.mapstruct package

Filip
  • 19,269
  • 7
  • 51
  • 60
0

No idea about disabling it, but why not do it like this?

@Mapper(componentModel = "spring")
abstract class UserRestMapper {
    public abstract UserDto map(User obj);
    
    public UserDto decoratedMap(User obj) {
        UserDto mapped = map(obj);
        // your after mapping logic
        return mapped;
    }
}
Shadov
  • 5,421
  • 2
  • 19
  • 38
  • It's too restricted. Sorry, I left out nested objects in the User that need to call the same @AfterMapping method... I have changed my minimal use case (it was a bit too minimal) – edbras Jul 15 '21 at 06:20