mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#3159 Do null value check for collections with default expression
This commit is contained in:
parent
1bc3436c5c
commit
86a668661a
@ -510,7 +510,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( defaultValue != null || defaultJavaExpression != null ) {
|
if ( hasDefaultValueOrDefaultExpression() ) {
|
||||||
// If there is default value defined then a check is needed
|
// If there is default value defined then a check is needed
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -518,6 +518,10 @@ public class PropertyMapping extends ModelElement {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasDefaultValueOrDefaultExpression() {
|
||||||
|
return defaultValue != null || defaultJavaExpression != null;
|
||||||
|
}
|
||||||
|
|
||||||
private Assignment assignToPlainViaAdder( Assignment rightHandSide) {
|
private Assignment assignToPlainViaAdder( Assignment rightHandSide) {
|
||||||
|
|
||||||
Assignment result = rightHandSide;
|
Assignment result = rightHandSide;
|
||||||
@ -555,7 +559,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
.targetAccessorType( targetAccessorType )
|
.targetAccessorType( targetAccessorType )
|
||||||
.rightHandSide( rightHandSide )
|
.rightHandSide( rightHandSide )
|
||||||
.assignment( rhs )
|
.assignment( rhs )
|
||||||
.nullValueCheckStrategy( nvcs )
|
.nullValueCheckStrategy( hasDefaultValueOrDefaultExpression() ? ALWAYS : nvcs )
|
||||||
.nullValuePropertyMappingStrategy( nvpms )
|
.nullValuePropertyMappingStrategy( nvpms )
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* 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._3159;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface Issue3159Mapper {
|
||||||
|
|
||||||
|
Issue3159Mapper INSTANCE = Mappers.getMapper( Issue3159Mapper.class );
|
||||||
|
|
||||||
|
@Mapping(target = "elements", defaultExpression = "java(new ArrayList<>())")
|
||||||
|
Target map(Source source);
|
||||||
|
|
||||||
|
default String elementName(Element element) {
|
||||||
|
return element != null ? element.getName() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Target {
|
||||||
|
private final Collection<String> elements;
|
||||||
|
|
||||||
|
public Target(Collection<String> elements) {
|
||||||
|
this.elements = elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<String> getElements() {
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Source {
|
||||||
|
private final Collection<Element> elements;
|
||||||
|
|
||||||
|
public Source(Collection<Element> elements) {
|
||||||
|
this.elements = elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Element> getElements() {
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Element {
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public Element(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* 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._3159;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("3159")
|
||||||
|
@WithClasses(Issue3159Mapper.class)
|
||||||
|
class Issue3159Test {
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
void shouldUseDefaultExpressionForCollection() {
|
||||||
|
Issue3159Mapper.Target target = Issue3159Mapper.INSTANCE.map( new Issue3159Mapper.Source( null ) );
|
||||||
|
|
||||||
|
assertThat( target.getElements() ).isEmpty();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user