Skip to content

Commit

Permalink
mapstruct#3747 Do not generate redundant if condition with constructo…
Browse files Browse the repository at this point in the history
…r mapping and RETURN_DEFAULT null value mapping strategy
  • Loading branch information
filiphr committed Nov 3, 2024
1 parent 32f1fea commit 21fdaa0
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
</#list>
</#if>
</#list>
<#else>
<#elseif !propertyMappingsByParameter(sourceParameters[0]).empty>
<#if mapNullToDefault>if ( <@includeModel object=getPresenceCheckByParameter(sourceParameters[0]) /> ) {</#if>
<#list propertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
<@includeModel object=propertyMapping targetBeanName=resultName existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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 );
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 21fdaa0

Please sign in to comment.