mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2937 Fix conditional check for collections with adders
This commit is contained in:
parent
237543c47c
commit
b24e831cf0
@ -64,6 +64,7 @@
|
||||
-->
|
||||
<#macro _assignment assignmentToUse>
|
||||
<@includeModel object=assignmentToUse
|
||||
presenceCheck=ext.presenceCheck
|
||||
targetBeanName=ext.targetBeanName
|
||||
existingInstanceMapping=ext.existingInstanceMapping
|
||||
targetReadAccessorName=ext.targetReadAccessorName
|
||||
|
@ -7,5 +7,6 @@
|
||||
-->
|
||||
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.MethodReferencePresenceCheck" -->
|
||||
<@includeModel object=methodReference
|
||||
presenceCheck=true
|
||||
targetPropertyName=ext.targetPropertyName
|
||||
targetType=ext.targetType/>
|
@ -6,4 +6,4 @@
|
||||
|
||||
-->
|
||||
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.common.SourceRHS" -->
|
||||
<#if sourceLoopVarName??>${sourceLoopVarName}<#elseif sourceLocalVarName??>${sourceLocalVarName}<#else>${sourceReference}</#if>
|
||||
<#if sourceLoopVarName?? && !ext.presenceCheck??>${sourceLoopVarName}<#elseif sourceLocalVarName??>${sourceLocalVarName}<#else>${sourceReference}</#if>
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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._2937;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.mapstruct.CollectionMappingStrategy;
|
||||
import org.mapstruct.Condition;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
|
||||
public interface Issue2937Mapper {
|
||||
|
||||
Issue2937Mapper INSTANCE = Mappers.getMapper( Issue2937Mapper.class );
|
||||
|
||||
Target map(Source source);
|
||||
|
||||
@Condition
|
||||
default boolean isApplicable(Collection<?> collection) {
|
||||
return collection == null || collection.size() > 1;
|
||||
}
|
||||
|
||||
class Source {
|
||||
private final Collection<String> names;
|
||||
|
||||
public Source(Collection<String> names) {
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
public Collection<String> getNames() {
|
||||
return names;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Target {
|
||||
private final Collection<String> names;
|
||||
|
||||
public Target() {
|
||||
this.names = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Collection<String> getNames() {
|
||||
return names;
|
||||
}
|
||||
|
||||
public void addName(String name) {
|
||||
this.names.add( name );
|
||||
}
|
||||
}
|
||||
}
|
@ -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._2937;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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("2937")
|
||||
@WithClasses({
|
||||
Issue2937Mapper.class,
|
||||
})
|
||||
class Issue2937Test {
|
||||
|
||||
@ProcessorTest
|
||||
void shouldCorrectlyUseConditionalForAdder() {
|
||||
List<String> sourceNames = new ArrayList<>();
|
||||
sourceNames.add( "Tester 1" );
|
||||
Issue2937Mapper.Source source = new Issue2937Mapper.Source( sourceNames );
|
||||
Issue2937Mapper.Target target = Issue2937Mapper.INSTANCE.map( source );
|
||||
|
||||
assertThat( target.getNames() ).isEmpty();
|
||||
|
||||
sourceNames.add( "Tester 2" );
|
||||
|
||||
target = Issue2937Mapper.INSTANCE.map( source );
|
||||
|
||||
assertThat( target.getNames() )
|
||||
.containsExactly( "Tester 1", "Tester 2" );
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user