DATAES-763 - Allow map properties in entity with null values.

Original PR: #405
This commit is contained in:
gsrinivas10 2020-03-22 12:59:32 +05:30 committed by GitHub
parent 745f9e9d79
commit a92970236c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 2 deletions

View File

@ -355,6 +355,11 @@ public class MappingElasticsearchConverter
for (Object value : source) {
if(value == null) {
return null;
}
if (isSimpleType(value)) {
target.add(
readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType));
@ -387,7 +392,9 @@ public class MappingElasticsearchConverter
Map<String, Object> target = new LinkedHashMap<>();
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(),
readSimpleValue(entry.getValue(), targetType.isMap() ? targetType.getComponentType() : targetType));
} else {

View File

@ -98,6 +98,7 @@ public class MappingElasticsearchConverterUnitTests {
Document rifleAsMap;
Document shotGunAsMap;
Document bigBunsCafeAsMap;
Document notificationAsMap;
@BeforeEach
public void init() {
@ -193,6 +194,17 @@ public class MappingElasticsearchConverterUnitTests {
shotGunAsMap = Document.create();
shotGunAsMap.put("model", "Ithaca 37 Pump Shotgun");
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
@ -616,6 +628,40 @@ public class MappingElasticsearchConverterUnitTests {
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) {
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;
}
@Data
static class Notification {
Long id;
String fromEmail;
String toEmail;
Map<String,Object> params;
}
@WritingConverter
static class ShotGunToMapConverter implements Converter<ShotGun, Map<String, Object>> {