From 333aba2c59f467921ea381f73f55eac65f69d47d Mon Sep 17 00:00:00 2001 From: Peter-Josef Meisch Date: Fri, 29 May 2020 19:09:08 +0200 Subject: [PATCH] DATAES-845 - MappingElasticsearchConverter handles lists with null values. Original PR: #470 (cherry picked from commit 852273eff5c06dbd9e1ef4bcd28d2736c482bdf9) --- .../MappingElasticsearchConverter.java | 14 ++++++----- ...appingElasticsearchConverterUnitTests.java | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java index 034fca418..b33a8d272 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java @@ -595,8 +595,14 @@ public class MappingElasticsearchConverter : Streamable.of(ObjectUtils.toObjectArray(value)); List 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 source) { diff --git a/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java b/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java index 486f3b417..56e3caa18 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java @@ -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 values; + } }