Issue
I migrate a project that use mapstruct from version 1.4.x to version 1.5.2.Final
In the existing code, there is a test that assert that if i try to map a null source to an existing target, the return should be null. This test no longer works.
After reading docs and post on stackoverflow, i set my mapping like this :
@Mapper(
componentModel = "spring",
nullValueMappingStrategy = RETURN_NULL,
nullValuePropertyMappingStrategy = SET_TO_NULL,
nullValueCheckStrategy = ALWAYS,
uses = {TariffDataMapper.class}
)
public interface ActDataMapper {
@Mappings({
@Mapping(target = "id", ignore = true),
@Mapping(target = "Tarifs", source = "source.capitalAtRisk.Tarifs"),
@Mapping(target = "invalidityTariffs", source = "source.capitalAtRisk.invalidityTariffs")
})
@BeanMapping( nullValueMappingStrategy = RETURN_NULL)
ActEntity updateEntity(Act source, @MappingTarget @NonNull ActEntity target);
I even try to override updateEntity method mapping with @BeanMapping, but the generated code is still the same :
@Override
public ActEntity updateEntity(Act source, ActEntity target) {
if ( source == null ) {
return target;
}
...
}
Where i expect the method to return null instead of target if source is null.
Could you please tell me where i'm wrong ?
Have a nice day.
Solution
When using update mappings with a return type then returned value will always be what was passed in @MappingTarget
. The nullValueMappingStrategy
does not have an impact on update mapping return values.
This is explained in the Updating existing bean instances section of the MapStruct documentation
Answered By - Filip
Answer Checked By - Robin (JavaFixing Admin)