mapstruct / mapstruct/mapstruct-examples

Mapper array to List example

Open
#121 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.4k
Forks
504
PR merge metrics
No merged PRs in 30d

Description

Hi,

Would be nice to have an example that shows the usage of MapStruct with array (like int[] ) to immutable list with specific add and addAll (like in protobuf).

example:
proto file

    message UserDTO {
        string id = 1;
        string email = 2;
        repeated PermissionDTO permissions = 3;
        repeated DepartmentDTO main_departments  = 4;
        repeated DepartmentDTO departments  = 5;
    }

java file

 public class User {

     private String id;
     private String email;
     private Permission[] permissions = new Permission[0];
     private List<Department> mainDepartments = new ArrayList<>();
     private List<Department> departments = new ArrayList<>();

     public String getId() {
         return id;
     }

     public void setId(String id) {
         this.id = id;
     }

     public String getEmail() {
         return email;
     }

     public void setEmail(String email) {
         this.email = email;
     }

     public Permission[] getPermissions() {
         return permissions;
     }

     public void setPermissions(Permission[] permissions) {
         this.permissions = permissions;
     }

public List<Department> getDepartments() {
	return departments;
}

public void setDepartments(List<Department> departments) {
	this.departments = departments;
}

public List<Department> getMainDepartments() {
	return mainDepartments;
}

public void setMainDepartments(List<Department> mainDepartments) {
	this.mainDepartments = mainDepartments;
}
 }

Mapper

 @Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
         nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
 public interface UserMapper {

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


     @Mapping(source = "permissions", target = "permissionsList")
     @Mapping(source = "mainDepartments", target = "mainDepartmentsList")
     @Mapping(source = "departments", target = "departmentsList")
     UserDTO map(User user);
 }

Error:
Can't map property "Permission[] permissions" to "PermissionDTO permissionsList".

### Desired solution:

 public class UserMapperImpl implements UserMapper {

     @Override
     public UserDTO map(User user) {
         if ( user == null ) {
             return null;
         }

         Builder userDTO = UserDTO.newBuilder();

         if ( user.getPermissions() != null ) {
             for ( Permission permission : user.getPermissions() ) {
                 userDTO.addPermissions( map( permission ) );
             }
         }
    
        ...

         return userDTO.build();
     }
 }

or

 public class UserMapperImpl implements UserMapper {

     @Override
     public UserDTO map(User user) {
         if ( user == null ) {
             return null;
         }

         Builder userDTO = UserDTO.newBuilder();

         if ( user.getPermissions() != null ) {
             userDTO.addAllPermissions( map( user.getPermissions()) );
         }
    
        ...

         return userDTO.build();
     }
 }

Is it possible to do this with actual release?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the UserMapper example and the generated UserMapperImpl shown in the issue, then inspect how the protobuf UserDTO builder exposes addPermissions and addAllPermissions. The example is done when it demonstrates mapping the Permission[] source to the protobuf repeated permissions field and includes the corresponding array-to-list behavior requested.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.