#3747 Do not generate redundant if condition with constructor mapping and RETURN_DEFAULT null value mapping strategy

This commit is contained in:
Filip Hrisafov 2024-11-03 12:54:37 +01:00
parent 32f1fea7b5
commit 21fdaa0f82
4 changed files with 100 additions and 1 deletions

View File

@ -116,7 +116,7 @@
</#list> </#list>
</#if> </#if>
</#list> </#list>
<#else> <#elseif !propertyMappingsByParameter(sourceParameters[0]).empty>
<#if mapNullToDefault>if ( <@includeModel object=getPresenceCheckByParameter(sourceParameters[0]) /> ) {</#if> <#if mapNullToDefault>if ( <@includeModel object=getPresenceCheckByParameter(sourceParameters[0]) /> ) {</#if>
<#list propertyMappingsByParameter(sourceParameters[0]) as propertyMapping> <#list propertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
<@includeModel object=propertyMapping targetBeanName=resultName existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/> <@includeModel object=propertyMapping targetBeanName=resultName existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>

View File

@ -0,0 +1,42 @@
/*
* 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._3747;
import org.mapstruct.Mapper;
import org.mapstruct.NullValueMappingStrategy;
/**
* @author Filip Hrisafov
*/
@Mapper(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface Issue3747Mapper {
Target map(Source source);
class Source {
private final String value;
public Source(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
class Target {
private final String value;
public Target(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}

View File

@ -0,0 +1,28 @@
/*
* 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._3747;
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;
import org.mapstruct.ap.testutil.runner.GeneratedSource;
/**
* @author Filip Hrisafov
*/
@IssueKey("3747")
@WithClasses(Issue3747Mapper.class)
class Issue3747Test {
@RegisterExtension
final GeneratedSource generatedSource = new GeneratedSource();
@ProcessorTest
void shouldNotGenerateObsoleteCode() {
generatedSource.addComparisonToFixtureFor( Issue3747Mapper.class );
}
}

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.bugs._3747;
import javax.annotation.processing.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-11-03T12:50:02+0100",
comments = "version: , compiler: javac, environment: Java 21.0.3 (N/A)"
)
public class Issue3747MapperImpl implements Issue3747Mapper {
@Override
public Target map(Source source) {
String value = null;
if ( source != null ) {
value = source.getValue();
}
Target target = new Target( value );
return target;
}
}