KJQ
KJQ

Reputation: 707

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);
}

Upvotes: 16

Views: 15666

Answers (3)

Xun Yang
Xun Yang

Reputation: 21

version

org.mapstruct:mapstruct-processor:1.5.5.Final

have some different

you can see source code in
org.mapstruct.ap.internal.util line 110

this.builderProvider = this.disableBuilder ?
  new NoOpBuilderProvider() :
  Services.get( BuilderProvider.class, defaultBuilderProvider );

So configuring spi about org.mapstruct.ap.spi.BuilderProvider file in the META-INF/services doesn't work anymore.

You must add processor compilerArgs in builde.gradle to make it choose 'NoOpBuilderProvider'

like this:

compileJava {
  options.compilerArgs += [
    '-Amapstruct.disableBuilders=true'
  ]
}

Upvotes: 2

ismile47
ismile47

Reputation: 631

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

Upvotes: 51

Filip
Filip

Reputation: 21423

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

Upvotes: 7

Related Questions