Skip to content

Commit 852273e

Browse files
authored
DATAES-845 - MappingElasticsearchConverter handles lists with null values.
Original PR: spring-projects#470
1 parent d26dfbb commit 852273e

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/main/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverter.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,14 @@ private Object writeCollectionValue(Object value, ElasticsearchPersistentPropert
595595
: Streamable.of(ObjectUtils.toObjectArray(value));
596596

597597
List<Object> target = new ArrayList<>();
598-
if (!typeHint.getActualType().getType().equals(Object.class) && isSimpleType(typeHint.getActualType().getType())) {
599-
collectionSource.map(this::getWriteSimpleValue).forEach(target::add);
598+
TypeInformation<?> actualType = typeHint.getActualType();
599+
Class<?> type = actualType != null ? actualType.getType() : null;
600+
601+
if (type != null && !type.equals(Object.class) && isSimpleType(type)) {
602+
// noinspection ReturnOfNull
603+
collectionSource //
604+
.map(element -> element != null ? getWriteSimpleValue(element) : null) //
605+
.forEach(target::add);
600606
} else {
601607

602608
collectionSource.map(it -> {
@@ -670,10 +676,6 @@ private boolean requiresTypeHint(TypeInformation<?> type, Class<?> actualType,
670676

671677
/**
672678
* Compute the type to use by checking the given entity against the store type;
673-
*
674-
* @param entity
675-
* @param source
676-
* @return
677679
*/
678680
private ElasticsearchPersistentEntity<?> computeClosestEntity(ElasticsearchPersistentEntity<?> entity,
679681
Map<String, Object> source) {

src/test/java/org/springframework/data/elasticsearch/core/convert/MappingElasticsearchConverterUnitTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,23 @@ void shouldNotReadSeqNoPrimaryTermProperty() {
718718
assertThat(entity.seqNoPrimaryTerm).isNull();
719719
}
720720

721+
@Test // DATAES-845
722+
void shouldWriteCollectionsWithNullValues() throws JSONException {
723+
EntityWithListProperty entity = new EntityWithListProperty();
724+
entity.setId("42");
725+
entity.setValues(Arrays.asList(null, "two", null, "four"));
726+
727+
String expected = '{' + //
728+
" \"id\": \"42\"," + //
729+
" \"values\": [null, \"two\", null, \"four\"]" + //
730+
'}';
731+
Document document = Document.create();
732+
mappingElasticsearchConverter.write(entity, document);
733+
String json = document.toJson();
734+
735+
assertEquals(expected, json, false);
736+
}
737+
721738
private String pointTemplate(String name, Point point) {
722739
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
723740
}
@@ -932,4 +949,11 @@ static class EntityWithSeqNoPrimaryTerm {
932949

933950
@Nullable private SeqNoPrimaryTerm seqNoPrimaryTerm;
934951
}
952+
953+
@Data
954+
static class EntityWithListProperty {
955+
@Id private String id;
956+
957+
private List<String> values;
958+
}
935959
}

0 commit comments

Comments
 (0)