Reputation: 3316
I have recently updated my spring boot version from 2.3.0.RELEASE to 2.4.4 and also updated the Map Struct version to the latest from 1.3.1.Final to is 1.4.2.Final. lombok version used - 1.18.18
But one of the mapper which was working fine with 1.3.1 is not working anymore with 1.4.2 and the scenario is as below.
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
......
<properties>
<!-- used for configuration meta data processor, please keep in sync with
spring boot parent version -->
<spring-boot.version>2.4.4</spring-boot.version>
<java.version>11</java.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
.....
</properties>
<dependencies>
........
........
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
........
........
</dependencies>
<build>
<finalName>dras_mt</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
........
........
</annotationProcessorPaths>
</configuration>
</plugin>
......
......
</plugins>
<pluginManagement>
.....
</pluginManagement>
</build>
</project>
Entity:
@Entity
@Data
public class TestEntity {
@Id
private Long id;
@NotNull
private String plntId;
@NotNull
private Boolean approvalNeeded = false;
}
Domain:
@Value
@Builder(toBuilder = true)
public class TestDomain {
private PlantDomain plantDomain;
@NonFinal
@Setter
private SomeInfo someInfo;
@NonFinal
@Setter
@Default
private Boolean approvalNeeded = false;
}
Mapper:
@Mapper(componentModel = "spring", uses = { ActaulPlantMapper.class })
public interface TestEntityMapper {
@Mapping(target = "someInfo", ignore = true)
@Mapping(source = "plntId", target = "plantDomain")
@Mapping(target = "approvalNeeded", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT)
TestDomain fromEntity(TestEntity entity);
}
Generated Code with MapStruct 1.3.1
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-03-24T11:33:38+0530",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.9.1 (AdoptOpenJDK)"
)
@Component
public class TestEntityMapperImpl implements TestEntityMapper {
@Autowired
private ActualPlantMapper actualPlantMapper;
@Override
public TestDomain fromEntity(WareHouseOptionEntity entity) {
if ( entity == null ) {
return null;
}
TestDomainBuilder testDomain = TestDomain.builder();
testDomain.plantDomain( actualPlantMapper.fromSapId( entity.getPlntId() ) );
if ( entity.getApprovalNeeded() != null ) {
testDomain.approvalNeeded( entity.getApprovalNeeded() );
}
else {
testDomain.approvalNeeded( false );
}
return testDomain.build();
}
}
Generated Code with MapStruct 1.4.2
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-03-24T11:33:38+0530",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.9.1 (AdoptOpenJDK)"
)
@Component
public class TestEntityMapperImpl implements TestEntityMapper {
@Autowired
private ActualPlantMapper actualPlantMapper;
@Override
public TestDomain fromEntity(WareHouseOptionEntity entity) {
if ( entity == null ) {
return null;
}
TestDomainBuilder testDomain = TestDomain.builder();
testDomain.plantDomain( actualPlantMapper.fromSapId( entity.getPlntId() ) );
testDomain.approvalNeeded( entity.getApprovalNeeded() );
return testDomain.build();
}
}
As you see with 1.4.2 version the NullValuePropertyMappingStrategy.SET_TO_DEFAULT is not applied.
I have tried with other options like putting the NullValuePropertyMappingStrategy on class level which didn't help.
Any futher inputs is highly appreaciated.
Upvotes: 1
Views: 2807
Reputation: 21471
The fact that NullValuePropertyMappingStrategy
was working on 1.3 is a coincidence. The strategy should only be applicable to update methods i.e. methods with @MappingTarget
.
If you want to keep defaults as defined in your source class I would suggest using NullValueCheckStrategy#ALWAYS
Upvotes: 1