Mapper array to List example
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
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.
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?
- Dominant language
- Java
- Stars
- 1.4k
- Forks
- 504
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mapstruct/mapstruct-examples
-
Java 21 support Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
mapstruct/mapstruct-examples#153 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
mapstruct/mapstruct-examples#149 · 1 comment ·
-
mapstruct-protobuf3 use lombok:UnsupportedOperationException(Collections$UnmodifiableCollection) Open
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
mapstruct/mapstruct-examples#142 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 15/100
mapstruct/mapstruct-examples#138 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
mapstruct/mapstruct-examples#128 · 1 reaction ·
All issues in mapstruct/mapstruct-examples
Similar issues
-
bug untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
opensearch-project/ml-commons#5094 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
emitter:client:csharp feature
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
affects/8.10 affects/8.9 component/clients kind/bug likelihood/mid severity/mid
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Two open-case totals on one screen: the Programs tile says 15,858 and the nav badge says 15,868 Openbug frontend maui-pilot
Difficulty 2/5 1-3 hours Newbie friendliness 72/100