Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapper array to List example #121

Open
hualmeid opened this issue Jul 28, 2021 · 3 comments
Open

Mapper array to List example #121

hualmeid opened this issue Jul 28, 2021 · 3 comments

Comments

@hualmeid
Copy link

hualmeid commented Jul 28, 2021

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?

@filiphr
Copy link
Member

filiphr commented Aug 14, 2021

Hey @hualmeid,

The issue list is not meant for asking questions, if you need to ask a question then please use StackOverflow.

Mapping between an array and List should work, so I would suggest to first try without protobuf and then see why it doesn't work with protobuf. If it is not working without protobuf please raise an issue in the mapstruct project.

@seime
Copy link

seime commented Aug 14, 2021

@hualmeid you might also want to have a look at https://github.com/entur/mapstruct-spi-protobuf for the protobuf part - no need to add specific mappings for repeated elements (which generates Lists in protobuf)

@filiphr
Copy link
Member

filiphr commented Aug 14, 2021

Good point @seime, thanks for pointing it out. Btw @seime if you want we could add the mapstruct-spi-protobuf in the MapStruct Documentation, feel free to add a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants