Skip to content

Commit 11ad678

Browse files
authored
BAEL-8307 Map empty string to null in MapStruct (#18467)
* BAEL-8307 Map empty string to null in MapStruct * BAEL-8307 Add example using targetPropertyName and sourcePropertyName * BAEL-8307 use source instead of target property * BAEL-8307 retrigger build
1 parent 47c5dad commit 11ad678

File tree

7 files changed

+138
-0
lines changed

7 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.mapstruct.emptystringtonull;
2+
3+
import org.mapstruct.Condition;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.factory.Mappers;
6+
7+
@Mapper
8+
public interface EmptyStringToNullCondition {
9+
10+
EmptyStringToNullCondition INSTANCE = Mappers.getMapper(EmptyStringToNullCondition.class);
11+
12+
Teacher toTeacher(Student student);
13+
14+
@Condition
15+
default boolean isNotEmpty(String value) {
16+
return value != null && !value.isEmpty();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mapstruct.emptystringtonull;
2+
3+
import org.mapstruct.Condition;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.TargetPropertyName;
6+
import org.mapstruct.SourcePropertyName;
7+
import org.mapstruct.factory.Mappers;
8+
9+
@Mapper
10+
public interface EmptyStringToNullConditionSourceProperty {
11+
12+
EmptyStringToNullConditionSourceProperty INSTANCE = Mappers.getMapper(EmptyStringToNullConditionSourceProperty.class);
13+
14+
Teacher toTeacher(Student student);
15+
16+
@Condition
17+
default boolean isNotEmpty(String value, @TargetPropertyName String targetPropertyName, @SourcePropertyName String sourcePropertyName) {
18+
if( sourcePropertyName.equals("lastName")) {
19+
return value != null && !value.isEmpty();
20+
}
21+
return true;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.mapstruct.emptystringtonull;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.Mapping;
5+
import org.mapstruct.factory.Mappers;
6+
7+
@Mapper
8+
public interface EmptyStringToNullExpression {
9+
EmptyStringToNullExpression INSTANCE = Mappers.getMapper(EmptyStringToNullExpression.class);
10+
11+
@Mapping(target = "lastName", expression = "java(student.lastName.isEmpty() ? null : student.lastName)")
12+
Teacher toTeacher(Student student);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.mapstruct.emptystringtonull;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.factory.Mappers;
5+
6+
@Mapper
7+
public interface EmptyStringToNullGlobal {
8+
9+
EmptyStringToNullGlobal INSTANCE = Mappers.getMapper(EmptyStringToNullGlobal.class);
10+
11+
Teacher toTeacher(Student student);
12+
13+
default String mapEmptyString(String string) {
14+
return string != null && !string.isEmpty() ? string : null;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.mapstruct.emptystringtonull;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class Student {
9+
String firstName;
10+
String lastName;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.mapstruct.emptystringtonull;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class Teacher {
9+
String firstName;
10+
String lastName;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.mapstruct.emptystringtonull;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class MapEmptyStringToNullUnitTest {
9+
10+
@Test
11+
void givenAMapperWithGlobalNullHandling_whenConvertingEmptyString_thenOutputNull() {
12+
EmptyStringToNullGlobal globalMapper = EmptyStringToNullGlobal.INSTANCE;
13+
Student student = new Student("Steve", "");
14+
Teacher teacher = globalMapper.toTeacher(student);
15+
assertEquals("Steve", teacher.firstName);
16+
assertNull(teacher.lastName);
17+
}
18+
19+
@Test
20+
void givenAMapperWithConditionAnnotationNullHandling_whenConvertingEmptyString_thenOutputNull() {
21+
EmptyStringToNullCondition conditionMapper = EmptyStringToNullCondition.INSTANCE;
22+
Student student = new Student("Steve", "");
23+
Teacher teacher = conditionMapper.toTeacher(student);
24+
assertEquals("Steve", teacher.firstName);
25+
assertNull(teacher.lastName);
26+
}
27+
28+
@Test
29+
void givenAMapperUsingExpressionBasedNullHandling_whenConvertingEmptyString_thenOutputNull() {
30+
EmptyStringToNullExpression expressionMapper = EmptyStringToNullExpression.INSTANCE;
31+
Student student = new Student("Steve", "");
32+
Teacher teacher = expressionMapper.toTeacher(student);
33+
assertEquals("Steve", teacher.firstName);
34+
assertNull(teacher.lastName);
35+
}
36+
37+
@Test
38+
void givenAMapperUsingConditionBasedNullHandlingWithPropertyNames_whenConvertingEmptyString_thenOutputNull() {
39+
EmptyStringToNullConditionSourceProperty expressionMapper = EmptyStringToNullConditionSourceProperty.INSTANCE;
40+
Student student = new Student("Steve", "");
41+
Teacher teacher = expressionMapper.toTeacher(student);
42+
assertEquals("Steve", teacher.firstName);
43+
assertNull(teacher.lastName);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)