#1826: fixed null pointer in nested property mapping when using presence checking (#1827)

This commit is contained in:
Jonathan Kraska 2019-05-26 11:41:44 -04:00 committed by Sjaak Derksen
parent d50e41cdbb
commit 119826982a
7 changed files with 126 additions and 1 deletions

View File

@ -23,6 +23,7 @@ Gervais Blaise - https://github.com/gervaisb
Gunnar Morling - https://github.com/gunnarmorling
Ivo Smid - https://github.com/bedla
Jeff Smyth - https://github.com/smythie86
Jonathan Kraska - https://github.com/jakraska
Joshua Spoerri - https://github.com/spoerri
Kevin Grüneberg - https://github.com/kevcodez
Michael Pardo - https://github.com/pardom

View File

@ -12,7 +12,7 @@
}
<#list propertyEntries as entry>
<#if entry.presenceCheckerName?? >
if ( !<@localVarName index=entry_index/>.${entry.presenceCheckerName}() ) {
if ( <#if entry_index != 0><@localVarName index=entry_index/> == null || </#if>!<@localVarName index=entry_index/>.${entry.presenceCheckerName}() ) {
return ${returnType.null};
}
</#if>

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._1826;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@Mapper
public interface Issue1826Mapper {
Issue1826Mapper INSTANCE = Mappers.getMapper( Issue1826Mapper.class );
@Mapping(target = "content", source = "sourceChild.content")
Target sourceAToTarget(SourceParent sourceParent);
}

View File

@ -0,0 +1,34 @@
/*
* 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._1826;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(AnnotationProcessorTestRunner.class)
@IssueKey("1826")
@WithClasses({
SourceParent.class,
SourceChild.class,
Target.class,
Issue1826Mapper.class
})
public class Issue1826Test {
@Test
public void testNestedPropertyMappingChecksForNull() {
SourceParent sourceParent = new SourceParent();
sourceParent.setSourceChild( null );
Target result = Issue1826Mapper.INSTANCE.sourceAToTarget( sourceParent );
assertThat( result.getContent() ).isNull();
}
}

View File

@ -0,0 +1,25 @@
/*
* 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._1826;
public class SourceChild {
private String content;
private Boolean hasContent = false;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
hasContent = true;
}
public Boolean hasContent() {
return hasContent;
}
}

View File

@ -0,0 +1,26 @@
/*
* 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._1826;
public class SourceParent {
private SourceChild sourceChild;
private Boolean hasSourceChild = false;
public SourceChild getSourceChild() {
return sourceChild;
}
public void setSourceChild(SourceChild sourceChild) {
this.sourceChild = sourceChild;
this.hasSourceChild = true;
}
public Boolean hasSourceChild() {
return hasSourceChild;
}
}

View File

@ -0,0 +1,19 @@
/*
* 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._1826;
public class Target {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}