#2758: fallback to param.variableName if ext.targetBeanName is not present in MethodReference handling. (#2759)

This commit is contained in:
Zegveld 2022-03-12 18:01:15 +01:00 committed by GitHub
parent 0b2c7e58b2
commit b6a3aa1512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 2 deletions

View File

@ -41,7 +41,7 @@
<#-- a class is passed on for casting, see @TargetType -->
<@includeModel object=inferTypeWhenEnum( ext.targetType ) raw=true/>.class<#t>
<#elseif param.mappingTarget>
${ext.targetBeanName}<#if ext.targetReadAccessorName??>.${ext.targetReadAccessorName}</#if><#t>
<#if ext.targetBeanName??>${ext.targetBeanName}<#else>${param.variableName}</#if><#if ext.targetReadAccessorName??>.${ext.targetReadAccessorName}</#if><#t>
<#elseif param.mappingContext>
${param.variableName}<#t>
<#elseif param.sourceRHS??>

View File

@ -7,8 +7,8 @@ package org.mapstruct.ap.test.conditional.basic;
import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
@ -223,4 +223,19 @@ public class ConditionalMappingTest {
}
@ProcessorTest
@WithClasses( {
ConditionalMethodWithMappingTargetInUpdateMapper.class
} )
@IssueKey( "2758" )
public void conditionalMethodWithMappingTarget() {
ConditionalMethodWithMappingTargetInUpdateMapper mapper =
ConditionalMethodWithMappingTargetInUpdateMapper.INSTANCE;
BasicEmployee targetEmployee = new BasicEmployee();
targetEmployee.setName( "CurrentName" );
mapper.map( new BasicEmployeeDto( "ReplacementName" ), targetEmployee );
assertThat( targetEmployee.getName() ).isEqualTo( "CurrentName" );
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.conditional.basic;
import org.mapstruct.Condition;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.factory.Mappers;
/**
* @author Ben Zegveld
*/
@Mapper( nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE )
public interface ConditionalMethodWithMappingTargetInUpdateMapper {
ConditionalMethodWithMappingTargetInUpdateMapper INSTANCE =
Mappers.getMapper( ConditionalMethodWithMappingTargetInUpdateMapper.class );
void map(BasicEmployeeDto employee, @MappingTarget BasicEmployee targetEmployee);
@Condition
default boolean isNotBlankAndNotPresent(String value, @MappingTarget BasicEmployee targetEmployee) {
return value != null && !value.trim().isEmpty() && targetEmployee.getName() == null;
}
}