mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-14 16:12:11 +00:00
DATAES-845 - MappingElasticsearchConverter handles lists with null values.
Original PR: #470 (cherry picked from commit 852273eff5c06dbd9e1ef4bcd28d2736c482bdf9)
This commit is contained in:
parent
e3e646eb72
commit
333aba2c59
@ -595,8 +595,14 @@ public class MappingElasticsearchConverter
|
|||||||
: Streamable.of(ObjectUtils.toObjectArray(value));
|
: Streamable.of(ObjectUtils.toObjectArray(value));
|
||||||
|
|
||||||
List<Object> target = new ArrayList<>();
|
List<Object> target = new ArrayList<>();
|
||||||
if (!typeHint.getActualType().getType().equals(Object.class) && isSimpleType(typeHint.getActualType().getType())) {
|
TypeInformation<?> actualType = typeHint.getActualType();
|
||||||
collectionSource.map(this::getWriteSimpleValue).forEach(target::add);
|
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 {
|
} else {
|
||||||
|
|
||||||
collectionSource.map(it -> {
|
collectionSource.map(it -> {
|
||||||
@ -670,10 +676,6 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute the type to use by checking the given entity against the store type;
|
* Compute the type to use by checking the given entity against the store type;
|
||||||
*
|
|
||||||
* @param entity
|
|
||||||
* @param source
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private ElasticsearchPersistentEntity<?> computeClosestEntity(ElasticsearchPersistentEntity<?> entity,
|
private ElasticsearchPersistentEntity<?> computeClosestEntity(ElasticsearchPersistentEntity<?> entity,
|
||||||
Map<String, Object> source) {
|
Map<String, Object> source) {
|
||||||
|
@ -718,6 +718,23 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertThat(entity.seqNoPrimaryTerm).isNull();
|
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) {
|
private String pointTemplate(String name, Point point) {
|
||||||
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
|
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;
|
@Nullable private SeqNoPrimaryTerm seqNoPrimaryTerm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
static class EntityWithListProperty {
|
||||||
|
@Id private String id;
|
||||||
|
|
||||||
|
private List<String> values;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user