Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private Assignment assignToPlainViaAdder( Assignment rightHandSide) {
Assignment result = rightHandSide;

String adderIteratorName = sourcePropertyName == null ? targetPropertyName : sourcePropertyName;
if ( result.getSourceType().isCollectionType() ) {
if ( result.getSourceType().isIterableType() ) {
result = new AdderWrapper( result, method.getThrownTypes(), isFieldAssignment(), adderIteratorName );
}
else if ( result.getSourceType().isStreamType() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ public AdderWrapper( Assignment rhs,
// localVar is iteratorVariable
String desiredName = Nouns.singularize( adderIteratorName );
rhs.setSourceLoopVarName( rhs.createUniqueVarName( desiredName ) );
adderType = first( getSourceType().determineTypeArguments( Collection.class ) );
if ( getSourceType().isCollectionType() ) {
adderType = first( getSourceType().determineTypeArguments( Collection.class ) );
}
else if ( getSourceType().isArrayType() ) {
adderType = getSourceType().getComponentType();
}
else { // iterable
adderType = first( getSourceType().determineTypeArguments( Iterable.class ) );
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public Type getSourceTypeForMatching() {
else if ( sourceType.isStreamType() ) {
return first( sourceType.determineTypeArguments( Stream.class ) );
}
else if ( sourceType.isArrayType() ) {
return sourceType.getComponentType();
}
else if ( sourceType.isIterableType() ) {
return first( sourceType.determineTypeArguments( Iterable.class ) );
}
}
return sourceType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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._3165;

import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface Issue3165Mapper {

Issue3165Mapper INSTANCE = Mappers.getMapper( Issue3165Mapper.class );

Target toTarget(Source source);

class Source {
private String[] pets;
private Iterable<String> cats;

public Source(String[] pets, Iterable<String> cats) {
this.pets = pets;
this.cats = cats;
}

public String[] getPets() {
return pets;
}

public Iterable<String> getCats() {
return cats;
}
}

class Target {
private List<String> pets;
private List<String> cats;

Target() {
this.pets = new ArrayList<>();
this.cats = new ArrayList<>();
}

public List<String> getPets() {
return pets;
}

public void addPet(String pet) {
pets.add( pet );
}

public List<String> getCats() {
return cats;
}

public void addCat(String cat) {
cats.add( cat );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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._3165;

import java.util.Arrays;

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;

@WithClasses({
Issue3165Mapper.class
})
@IssueKey("3165")
class Issue3165MapperTest {

@ProcessorTest
void supportsAdderWhenMappingArrayAndIterableToCollection() {
Issue3165Mapper.Source src = new Issue3165Mapper.Source(
new String[] { "cat", "dog", "mouse" },
Arrays.asList( "ivy", "flu", "freya" )
);
Issue3165Mapper.Target target = Issue3165Mapper.INSTANCE.toTarget( src );
assertThat( target.getPets() ).containsExactly( "cat", "dog", "mouse" );
assertThat( target.getCats() ).containsExactly( "ivy", "flu", "freya" );
}
}