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

This commit is contained in:
Peter-Josef Meisch 2020-03-22 08:58:11 +01:00
parent 39b318caa7
commit 54c80f3375

View File

@ -15,8 +15,6 @@
*/ */
package org.springframework.data.elasticsearch.core; package org.springframework.data.elasticsearch.core;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.io.IOException; import java.io.IOException;
@ -52,6 +50,8 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
/** /**
* Elasticsearch specific {@link EntityReader} & {@link EntityWriter} implementation based on domain type * Elasticsearch specific {@link EntityReader} & {@link EntityWriter} implementation based on domain type
@ -241,27 +241,31 @@ public class ElasticsearchEntityMapper implements
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())) { String entryKey = entry.getKey();
target.put(entry.getKey(), Object entryValue = entry.getValue();
readSimpleValue(entry.getValue(), targetType.isMap() ? targetType.getComponentType() : targetType));
if (entryValue == null) {
target.put(entryKey, null);
} else if (isSimpleType(entryValue)) {
target.put(entryKey,
readSimpleValue(entryValue, targetType.isMap() ? targetType.getComponentType() : targetType));
} else { } else {
ElasticsearchPersistentEntity<?> targetEntity = computeGenericValueTypeForRead(property, entry.getValue()); ElasticsearchPersistentEntity<?> targetEntity = computeGenericValueTypeForRead(property, entryValue);
if (targetEntity.getTypeInformation().isMap()) { if (targetEntity.getTypeInformation().isMap()) {
Map<String, Object> valueMap = (Map) entry.getValue(); Map<String, Object> valueMap = (Map) entryValue;
if (typeMapper.containsTypeInformation(valueMap)) { if (typeMapper.containsTypeInformation(valueMap)) {
target.put(entry.getKey(), readEntity(targetEntity, (Map) entry.getValue())); target.put(entryKey, readEntity(targetEntity, (Map) entryValue));
} else { } else {
target.put(entry.getKey(), readValue(valueMap, property, targetEntity.getTypeInformation())); target.put(entryKey, readValue(valueMap, property, targetEntity.getTypeInformation()));
} }
} else if (targetEntity.getTypeInformation().isCollectionLike()) { } else if (targetEntity.getTypeInformation().isCollectionLike()) {
target.put(entry.getKey(), target.put(entryKey, readValue(entryValue, property, targetEntity.getTypeInformation().getActualType()));
readValue(entry.getValue(), property, targetEntity.getTypeInformation().getActualType()));
} else { } else {
target.put(entry.getKey(), readEntity(targetEntity, (Map) entry.getValue())); target.put(entryKey, readEntity(targetEntity, (Map) entryValue));
} }
} }
} }
@ -281,7 +285,9 @@ public class ElasticsearchEntityMapper implements
for (Object value : source) { for (Object value : source) {
if (isSimpleType(value)) { if (value == null) {
target.add(null);
} else if (isSimpleType(value)) {
target.add( target.add(
readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType)); readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType));
} else { } else {
@ -493,7 +499,14 @@ public class ElasticsearchEntityMapper implements
if (!typeHint.getActualType().getType().equals(Object.class) if (!typeHint.getActualType().getType().equals(Object.class)
&& isSimpleType(typeHint.getMapValueType().getType())) { && isSimpleType(typeHint.getMapValueType().getType())) {
mapSource.forEach(it -> target.put(it.getKey(), getWriteSimpleValue(it.getValue()))); mapSource.forEach(it -> {
if (it.getValue() == null) {
target.put(it.getKey(), null);
} else {
target.put(it.getKey(), getWriteSimpleValue(it.getValue()));
}
});
} else { } else {
mapSource.forEach(it -> { mapSource.forEach(it -> {