mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-27 14:22:30 +00:00
DATAES-763 - Allow map properties in entity with null values.
Original PR: #405
This commit is contained in:
parent
745f9e9d79
commit
a92970236c
@ -355,6 +355,11 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
for (Object value : source) {
|
for (Object value : source) {
|
||||||
|
|
||||||
|
if(value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (isSimpleType(value)) {
|
if (isSimpleType(value)) {
|
||||||
target.add(
|
target.add(
|
||||||
readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType));
|
readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType));
|
||||||
@ -387,7 +392,9 @@ public class MappingElasticsearchConverter
|
|||||||
Map<String, Object> target = new LinkedHashMap<>();
|
Map<String, Object> target = new LinkedHashMap<>();
|
||||||
for (Entry<String, Object> entry : source.entrySet()) {
|
for (Entry<String, Object> entry : source.entrySet()) {
|
||||||
|
|
||||||
if (isSimpleType(entry.getValue())) {
|
if(entry.getValue() == null) {
|
||||||
|
target.put(entry.getKey(),null);
|
||||||
|
} else if (isSimpleType(entry.getValue())) {
|
||||||
target.put(entry.getKey(),
|
target.put(entry.getKey(),
|
||||||
readSimpleValue(entry.getValue(), targetType.isMap() ? targetType.getComponentType() : targetType));
|
readSimpleValue(entry.getValue(), targetType.isMap() ? targetType.getComponentType() : targetType));
|
||||||
} else {
|
} else {
|
||||||
|
@ -98,6 +98,7 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
Document rifleAsMap;
|
Document rifleAsMap;
|
||||||
Document shotGunAsMap;
|
Document shotGunAsMap;
|
||||||
Document bigBunsCafeAsMap;
|
Document bigBunsCafeAsMap;
|
||||||
|
Document notificationAsMap;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void init() {
|
public void init() {
|
||||||
@ -193,6 +194,17 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
shotGunAsMap = Document.create();
|
shotGunAsMap = Document.create();
|
||||||
shotGunAsMap.put("model", "Ithaca 37 Pump Shotgun");
|
shotGunAsMap.put("model", "Ithaca 37 Pump Shotgun");
|
||||||
shotGunAsMap.put("_class", ShotGun.class.getName());
|
shotGunAsMap.put("_class", ShotGun.class.getName());
|
||||||
|
|
||||||
|
notificationAsMap = Document.create();
|
||||||
|
notificationAsMap.put("id",1L);
|
||||||
|
notificationAsMap.put("fromEmail","from@email.com");
|
||||||
|
notificationAsMap.put("toEmail","to@email.com");
|
||||||
|
Map<String,Object> data = new HashMap<>();
|
||||||
|
data.put("documentType","abc");
|
||||||
|
data.put("content",null);
|
||||||
|
notificationAsMap.put("params",data);
|
||||||
|
notificationAsMap.put("_class",
|
||||||
|
"org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$Notification");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -616,6 +628,40 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertThat(person.getGender()).isEqualTo(Gender.MAN);
|
assertThat(person.getGender()).isEqualTo(Gender.MAN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test //DATAES-763
|
||||||
|
void writeEntityWithMapDataType() {
|
||||||
|
|
||||||
|
Notification notification = new Notification();
|
||||||
|
notification.fromEmail="from@email.com";
|
||||||
|
notification.toEmail="to@email.com";
|
||||||
|
Map<String,Object> data = new HashMap<>();
|
||||||
|
data.put("documentType","abc");
|
||||||
|
data.put("content",null);
|
||||||
|
notification.params= data;
|
||||||
|
notification.id= 1L;
|
||||||
|
|
||||||
|
Document document = Document.create();
|
||||||
|
mappingElasticsearchConverter.write(notification,document);
|
||||||
|
assertThat(document).isEqualTo(notificationAsMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test //DATAES-763
|
||||||
|
void readEntityWithMapDataType() {
|
||||||
|
|
||||||
|
Document document = Document.create();
|
||||||
|
document.put("id",1L);
|
||||||
|
document.put("fromEmail","from@email.com");
|
||||||
|
document.put("toEmail","to@email.com");
|
||||||
|
Map<String,Object> data = new HashMap<>();
|
||||||
|
data.put("documentType","abc");
|
||||||
|
data.put("content",null);
|
||||||
|
document.put("params",data);
|
||||||
|
|
||||||
|
Notification notification = mappingElasticsearchConverter.read(Notification.class,document);
|
||||||
|
assertThat(notification.params.get("documentType")).isEqualTo("abc");
|
||||||
|
assertThat(notification.params.get("content")).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
private String pointTemplate(String name, Point point) {
|
private String pointTemplate(String name, Point point) {
|
||||||
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
|
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
|
||||||
}
|
}
|
||||||
@ -749,6 +795,15 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
Map<String, Object> objectMap;
|
Map<String, Object> objectMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
static class Notification {
|
||||||
|
|
||||||
|
Long id;
|
||||||
|
String fromEmail;
|
||||||
|
String toEmail;
|
||||||
|
Map<String,Object> params;
|
||||||
|
}
|
||||||
|
|
||||||
@WritingConverter
|
@WritingConverter
|
||||||
static class ShotGunToMapConverter implements Converter<ShotGun, Map<String, Object>> {
|
static class ShotGunToMapConverter implements Converter<ShotGun, Map<String, Object>> {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user