Repository navigation
Open
Labels
Description
Mapstruct create two helper methods stringArrayToStringList and stringArrayToStringList1 to convert a String-array to a String-list when one property is automappable and and another property is not. It does not break anything and the mapper still works. It is just a little bit ugly. When all fields are automappable or all fields are explicit mapped, Mapstruct only creates one helper method.
Version: 1.3.1.Final
DTO-class:
package one;
import lombok.Data;
@Data
public class AnimalDto {
private String[] names;
private String[] otherNames;
}
Entity-class:
package one;
import lombok.Data;
import java.util.List;
@Data
public class Animal {
private List<String> names;
private List<String> specialNames;
}
Mapper-Interface:
package two;
import one.Animal;
import one.AnimalDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper
public interface AnimalMapper {
@Mapping(target = "specialNames", source = "otherNames")
Animal dtoToEntity(AnimalDto dto);
}
Generated Mapper-Class
package two;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import one.Animal;
import one.AnimalDto;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-11-11T14:06:51+0100",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_192 (Oracle Corporation)"
)
public class AnimalMapperImpl implements AnimalMapper {
@Override
public Animal dtoToEntity(AnimalDto dto) {
if ( dto == null ) {
return null;
}
Animal animal = new Animal();
animal.setSpecialNames( stringArrayToStringList( dto.getOtherNames() ) );
animal.setNames( stringArrayToStringList1( dto.getNames() ) );
return animal;
}
protected List<String> stringArrayToStringList(String[] stringArray) {
if ( stringArray == null ) {
return null;
}
List<String> list = new ArrayList<String>( stringArray.length );
for ( String string : stringArray ) {
list.add( string );
}
return list;
}
protected List<String> stringArrayToStringList1(String[] stringArray) {
if ( stringArray == null ) {
return null;
}
List<String> list = new ArrayList<String>( stringArray.length );
for ( String string : stringArray ) {
list.add( string );
}
return list;
}
}
Reacted by ttzn
Activity
ttzn commentedon Nov 27, 2019
I noticed this too. It looks like the first generated method is not reused because this test in
ContainerMappingMethod::equalsfails:In the provided example, for the explicit mapping of
specialNames,selectionParametersis an object with empty collections and null fields, while in the case of an implicit, name-based mapping likenames, it'snull. I could fix it by assigning "empty shell"SelectionParametersobjects in theapplyPropertyNameBasedMapping()procedure to allow matches, but maybe there's a cleaner way.mapstruct#1968 Do not pass condition qualifiers to forged iterable ma…
Maver4ck commentedon Nov 24, 2026
I looked into this on the current
main(1.7.0-SNAPSHOT).The exact scenario from the issue no longer reproduces. It was fixed as a side effect of #3591 (commit 3047760), which made
ContainerMappingMethodtreatnullselection parameters asSelectionParameters.empty().However, the same mechanism still leads to duplicates when the explicit mapping uses a condition qualifier. The property-level selection parameters, including
conditionQualifiedBy/conditionQualifiedByName, are passed on to the forged iterable mapping method and are part of itsequals. The condition qualifiers only select the presence check of the property itself (generated in the calling method), so they have no influence on the forged method, but they make otherwise identical forged methods unequal.Reproducer
AnimalDtoandAnimalare the classes from the issue description.Generated code
Before (current
main): two byte-for-byte identical helper methods are generated.Issue1968ConditionMapperImpl (before)
After (#4158): a single helper is reused, and the condition is still applied to the property itself.
Issue1968ConditionMapperImpl (after)
The "before" output can be reproduced by running
Issue1968Testwithout the change toPropertyMapping. The expected "after" output is checked in as a fixture (Issue1968ConditionMapperImpl.java).