Skip to content

Commit 5617f3a

Browse files
Fixed primitive early return if all ref types are null
Signed-off-by: ronodhirSoumik <ronodhirsoumik@gmail.com>
1 parent 5880067 commit 5617f3a

3 files changed

Lines changed: 83 additions & 1 deletion

File tree

processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
</#if>
2828
</#list>
29-
<#if !mapNullToDefault && !sourcePresenceChecks.empty>
29+
<#if !mapNullToDefault && sourcePresenceChecks?has_content && sourcePresenceChecks?size == sourceParameters?size>
3030
if ( <#list sourcePresenceChecks as sourcePresenceCheck><@includeModel object=sourcePresenceCheck.negate() /><#if sourcePresenceCheck_has_next> && </#if></#list> ) {
3131
<#if returnType.name == "void">
3232
return;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._3992;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.factory.Mappers;
10+
11+
/**
12+
* @author Soumik Sarker
13+
*/
14+
@Mapper
15+
public interface MultiSourceWithPrimitiveMapper {
16+
17+
MultiSourceWithPrimitiveMapper INSTANCE = Mappers.getMapper( MultiSourceWithPrimitiveMapper.class );
18+
19+
Target map(String sku, String name, Long categoryId, int size, int pageNumber, boolean draft);
20+
21+
class Target {
22+
private String sku;
23+
private String name;
24+
private Long categoryId;
25+
private int size;
26+
private int pageNumber;
27+
private boolean draft;
28+
29+
// Getters and Setters
30+
public String getSku() { return sku; }
31+
public void setSku(String sku) { this.sku = sku; }
32+
public String getName() { return name; }
33+
public void setName(String name) { this.name = name; }
34+
public Long getCategoryId() { return categoryId; }
35+
public void setCategoryId(Long categoryId) { this.categoryId = categoryId; }
36+
public int getSize() { return size; }
37+
public void setSize(int size) { this.size = size; }
38+
public int getPageNumber() { return pageNumber; }
39+
public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; }
40+
public boolean isDraft() { return draft; }
41+
public void setDraft(boolean draft) { this.draft = draft; }
42+
}
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._3992;
7+
8+
import org.mapstruct.ap.testutil.IssueKey;
9+
import org.mapstruct.ap.testutil.ProcessorTest;
10+
import org.mapstruct.ap.testutil.WithClasses;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
/**
15+
* Test for primitive parameters when generating early return
16+
*
17+
* @author Soumik Sarker
18+
*/
19+
public class MultiSourceWithPrimitiveMapperTest {
20+
21+
@IssueKey("3992")
22+
@ProcessorTest
23+
@WithClasses({
24+
MultiSourceWithPrimitiveMapper.class
25+
})
26+
void shouldMapPrimitivesEvenIfAllReferenceParametersAreNull() {
27+
MultiSourceWithPrimitiveMapper.Target target = MultiSourceWithPrimitiveMapper.INSTANCE.map(
28+
null, null, null, 10, 2, true
29+
);
30+
31+
assertThat( target ).isNotNull();
32+
assertThat( target.getSku() ).isNull();
33+
assertThat( target.getName() ).isNull();
34+
assertThat( target.getCategoryId() ).isNull();
35+
assertThat( target.getSize() ).isEqualTo( 10 );
36+
assertThat( target.getPageNumber() ).isEqualTo( 2 );
37+
assertThat( target.isDraft() ).isTrue();
38+
}
39+
}

0 commit comments

Comments
 (0)