#3809 Fix conditional mapping with @TargetPropertyName failing for nested update mappings

Signed-off-by: TangYang <tangyang9464@163.com>
This commit is contained in:
Yang Tang 2025-05-25 21:40:43 +08:00 committed by GitHub
parent 42c87d1da9
commit 3a5c70224d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 103 additions and 1 deletions

View File

@ -10,7 +10,7 @@
<@lib.handleExceptions>
<#if includeSourceNullCheck>
<@lib.sourceLocalVarAssignment/>
if ( <#if sourcePresenceCheckerReference?? ><@includeModel object=sourcePresenceCheckerReference /><#else><#if sourceLocalVarName??>${sourceLocalVarName}<#else>${sourceReference}</#if> != null</#if> ) {
if ( <@handleSourceReferenceNullCheck/> ) {
<@assignToExistingTarget/>
<@lib.handleAssignment/>;
}
@ -32,3 +32,16 @@
${ext.targetBeanName}.${ext.targetWriteAccessorName}<@lib.handleWrite><@lib.initTargetObject/></@lib.handleWrite>;
}
</#macro>
<#macro handleSourceReferenceNullCheck>
<@compress single_line=true>
<#if sourcePresenceCheckerReference?? >
<@includeModel object=sourcePresenceCheckerReference
targetPropertyName=ext.targetPropertyName
sourcePropertyName=ext.sourcePropertyName
targetType=ext.targetType/>
<#else>
<#if sourceLocalVarName??> ${sourceLocalVarName} <#else> ${sourceReference} </#if> != null
</#if>
</@compress>
</#macro>

View File

@ -0,0 +1,69 @@
/*
* 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.bugs._3809;
import org.mapstruct.Condition;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.TargetPropertyName;
@Mapper
public interface Issue3809Mapper {
void updateMappingFails(Source source, @MappingTarget Target target);
@Condition
default boolean canMap(Object source, @TargetPropertyName String propertyName) {
return true;
}
class Source {
private NestedSource param;
public NestedSource getParam() {
return param;
}
}
class NestedSource {
private String param1;
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
}
class Target {
private NestedTarget param;
public NestedTarget getParam() {
return param;
}
public void setParam(NestedTarget param) {
this.param = param;
}
}
class NestedTarget {
private String param1;
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
}
}

View File

@ -0,0 +1,20 @@
/*
* 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.bugs._3809;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
@WithClasses(Issue3809Mapper.class)
@IssueKey("3809")
public class Issue3809Test {
@ProcessorTest
public void shouldCompileNoError() {
}
}