'Mapstruct map list to an object containing list

I want to have a 2 way mapping from a List of an object to an Object which contains a List.

class Person {
  String firstName;
  String lastName;
}

class Group { // Source
  List<Person> people;
  String groupID; 
  ...
}

class Employee { // target
  String firstName;
  String lastName;
  String employeeNumber;
  ...
}

I used ReportingPolicy.IGNORED to ignore all the irrelevant fields. I just want a mapping between Group to List with the fields firstName and lastName.

Is it possible at all? I have tried but it's giving me error during build "impossible to map iterable to non-iterable."

@Mapping(target="people", source".")
Group map(List<Employee>)


Solution 1:[1]

MapStruct supports mapping from a source parameter to a target parameter. This means that you can configure MapStruct to map the source list parameter to a target list property. However, as pointed out in https://stackoverflow.com/a/71181377/1115491 it is not possible to map form a single collection parameter to a target parameter. The only way it works is if there are multiple source parameters.

e.g.

@Mapper
public interface Mapper {

    @Mapping(target = "people", source = "employees")
    @Mapping(target = "groupID", source = "groupId")
    Group map(String groupId, List<Employee> employees);

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1