DATAES-845 - MappingElasticsearchConverter handles lists with null values.

Original PR: #470
This commit is contained in:
Peter-Josef Meisch 2020-05-29 19:09:08 +02:00 committed by GitHub
parent d26dfbba70
commit 852273eff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 6 deletions

View File

@ -595,8 +595,14 @@ public class MappingElasticsearchConverter
: Streamable.of(ObjectUtils.toObjectArray(value));
List<Object> target = new ArrayList<>();
if (!typeHint.getActualType().getType().equals(Object.class) && isSimpleType(typeHint.getActualType().getType())) {
collectionSource.map(this::getWriteSimpleValue).forEach(target::add);
TypeInformation<?> actualType = typeHint.getActualType();
Class<?> type = actualType != null ? actualType.getType() : null;
if (type != null && !type.equals(Object.class) && isSimpleType(type)) {
// noinspection ReturnOfNull
collectionSource //
.map(element -> element != null ? getWriteSimpleValue(element) : null) //
.forEach(target::add);
} else {
collectionSource.map(it -> {
@ -670,10 +676,6 @@ public class MappingElasticsearchConverter
/**
* Compute the type to use by checking the given entity against the store type;
*
* @param entity
* @param source
* @return
*/
private ElasticsearchPersistentEntity<?> computeClosestEntity(ElasticsearchPersistentEntity<?> entity,
Map<String, Object> source) {

View File

@ -718,6 +718,23 @@ public class MappingElasticsearchConverterUnitTests {
assertThat(entity.seqNoPrimaryTerm).isNull();
}
@Test // DATAES-845
void shouldWriteCollectionsWithNullValues() throws JSONException {
EntityWithListProperty entity = new EntityWithListProperty();
entity.setId("42");
entity.setValues(Arrays.asList(null, "two", null, "four"));
String expected = '{' + //
" \"id\": \"42\"," + //
" \"values\": [null, \"two\", null, \"four\"]" + //
'}';
Document document = Document.create();
mappingElasticsearchConverter.write(entity, document);
String json = document.toJson();
assertEquals(expected, json, false);
}
private String pointTemplate(String name, Point point) {
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
}
@ -932,4 +949,11 @@ public class MappingElasticsearchConverterUnitTests {
@Nullable private SeqNoPrimaryTerm seqNoPrimaryTerm;
}
@Data
static class EntityWithListProperty {
@Id private String id;
private List<String> values;
}
}