Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn off "builders" in MapStruct when using Immutables

How do I completely disable using "builders" in MapStruct? I don't want to use them at all as they are causing all kinds of issues for me.

I created the service file under META-INF (I would prefer a way to assign it to the mapping builder= but I did not see any examples how to do it right in code).

It is still trying to use Immutables "builder" instance instance of the "ModifiableXXX" instance I want to map to. I'd even take a way of forcing it to the modifiable type if that is available.

In another mapping, using an update the ModifiableXXX (with @AfterMapping and @MappingTarget) approach works.

My mapper looks like this right now:

@Mapper
public interface MongoProjectMapper
{
    ModifiableProject mapModel(MongoProject project);

    @AfterMapping
    ModifiableProject updateProject(MongoEntity e, @MappingTarget ModifiableProject p);
}
like image 698
KJQ Avatar asked Feb 12 '19 17:02

KJQ


People also ask

What is MapStruct AfterMapping?

Annotation Type AfterMappingMarks a method to be invoked at the end of a generated mapping method, right before the last return statement of the mapping method. The method can be implemented in an abstract mapper class, be declared in a type (class or interface) referenced in Mapper.

Does MapStruct use reflection?

During compilation, MapStruct will generate an implementation of this interface. This implementation uses plain Java method invocations for mapping between source and target objects, i.e. no reflection or similar.

Why do we use MapStruct?

MapStruct is a code generator tool that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach. The generated mapping code uses plain method invocations and thus is fast, type-safe, and easy to understand.

Does MapStruct work with Lombok?

Can I use MapStruct together with Project Lombok? Yes, as of MapStruct 1.2. 0. Beta1 and Lombok 1.16.


2 Answers

From Mapstruct version 1.3.1.Final we can use annotation org.mapstruct.Builder#disableBuilder within: @BeanMapping, @Mapper or @MapperConfig

@Mapper(builder = @Builder(disableBuilder = true))
public interface ProjectMapper

Have a look at #mapping-with-builders and documentation

like image 185
ismile47 Avatar answered Sep 19 '22 14:09

ismile47


Completely disabling builders is possible via the NoOpBuilderProvider. You need to create a org.mapstruct.ap.spi.BuilderProvider file in the META-INF/services directory with org.mapstruct.ap.spi.NoOpBuilderProvider as it’s content. This will completely disable the builders.

There is a feature request to make this more granular and disable it via @BeanMapping or on the mapper level. Have a look at mapstruct/mapstruct#1661

like image 43
Filip Avatar answered Sep 18 '22 14:09

Filip