Skip to content

MapStruct creates duplicate helper methods for Array to List conversion #1968

Open

Description

@serofax

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;
    }
}

Activity

  1. ttzn commented on Nov 27, 2019

    @ttzn
    Contributor

    I noticed this too. It looks like the first generated method is not reused because this test in ContainerMappingMethod::equals fails:

            if ( !Objects.equals( selectionParameters, other.selectionParameters ) ) {
                return false;
            }
    

    In the provided example, for the explicit mapping of specialNames, selectionParameters is an object with empty collections and null fields, while in the case of an implicit, name-based mapping like names, it's null. I could fix it by assigning "empty shell" SelectionParameters objects in the applyPropertyNameBasedMapping() procedure to allow matches, but maybe there's a cleaner way.

  2. added a commit that references this issue on Nov 24, 2026
  3. Maver4ck commented on Nov 24, 2026

    @Maver4ck

    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 ContainerMappingMethod treat null selection parameters as SelectionParameters.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 its equals. 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

    @Mapper
    public interface Issue1968ConditionMapper {
    
        @Mapping(target = "specialNames", source = "otherNames", conditionQualifiedByName = "notEmpty")
        Animal dtoToEntity(AnimalDto dto);
    
        @Condition
        @Named("notEmpty")
        default boolean isNotEmpty(String[] values) {
            return values != null && values.length > 0;
        }
    }

    AnimalDto and Animal are the classes from the issue description.

    Generated code

    Before (current main): two byte-for-byte identical helper methods are generated.

    Issue1968ConditionMapperImpl (before)
    public class Issue1968ConditionMapperImpl implements Issue1968ConditionMapper {
    
        @Override
        public Animal dtoToEntity(AnimalDto dto) {
            if ( dto == null ) {
                return null;
            }
    
            Animal animal = new Animal();
    
            if ( isNotEmpty( dto.getOtherNames() ) ) {
                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<>( 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<>( stringArray.length );
            for ( String string : stringArray ) {
                list.add( string );
            }
    
            return list;
        }
    }

    After (#4158): a single helper is reused, and the condition is still applied to the property itself.

    Issue1968ConditionMapperImpl (after)
    public class Issue1968ConditionMapperImpl implements Issue1968ConditionMapper {
    
        @Override
        public Animal dtoToEntity(AnimalDto dto) {
            if ( dto == null ) {
                return null;
            }
    
            Animal animal = new Animal();
    
            if ( isNotEmpty( dto.getOtherNames() ) ) {
                animal.setSpecialNames( stringArrayToStringList( dto.getOtherNames() ) );
            }
            animal.setNames( stringArrayToStringList( dto.getNames() ) );
    
            return animal;
        }
    
        protected List<String> stringArrayToStringList(String[] stringArray) {
            if ( stringArray == null ) {
                return null;
            }
    
            List<String> list = new ArrayList<>( stringArray.length );
            for ( String string : stringArray ) {
                list.add( string );
            }
    
            return list;
        }
    }

    The "before" output can be reproduced by running Issue1968Test without the change to PropertyMapping. The expected "after" output is checked in as a fixture (Issue1968ConditionMapperImpl.java).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions