Skip to content

Commit 407c8c6

Browse files
authored
DATAES-857 - Registered simple types are not read from list.
Original PR: spring-projects#478
1 parent 407da04 commit 407c8c6

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,17 @@ private <R> R readCollectionValue(@Nullable List<?> source, ElasticsearchPersist
316316
}
317317

318318
Collection<Object> target = createCollectionForValue(targetType, source.size());
319+
TypeInformation<?> componentType = targetType.getComponentType();
319320

320321
for (Object value : source) {
321322

322323
if (value == null) {
323324
target.add(null);
325+
} else if (componentType != null && !ClassTypeInformation.OBJECT.equals(componentType)
326+
&& isSimpleType(componentType.getType())) {
327+
target.add(readSimpleValue(value, componentType));
324328
} else if (isSimpleType(value)) {
325-
target.add(
326-
readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType));
329+
target.add(readSimpleValue(value, componentType != null ? componentType : targetType));
327330
} else {
328331

329332
if (value instanceof List) {

src/main/java/org/springframework/data/elasticsearch/core/geo/GeoPoint.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import org.springframework.data.geo.Point;
1919

20+
import java.util.Objects;
21+
2022
/**
2123
* geo-location used for #{@link org.springframework.data.elasticsearch.core.query.Criteria}.
2224
*
@@ -60,6 +62,20 @@ public static Point toPoint(GeoPoint point) {
6062
return new Point(point.getLat(), point.getLon());
6163
}
6264

65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) return true;
68+
if (o == null || getClass() != o.getClass()) return false;
69+
GeoPoint geoPoint = (GeoPoint) o;
70+
return Double.compare(geoPoint.lat, lat) == 0 &&
71+
Double.compare(geoPoint.lon, lon) == 0;
72+
}
73+
74+
@Override
75+
public int hashCode() {
76+
return Objects.hash(lat, lon);
77+
}
78+
6379
@Override
6480
public String toString() {
6581
return "GeoPoint{" +

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,60 @@ void shouldWriteCollectionsWithNullValues() throws JSONException {
735735
assertEquals(expected, json, false);
736736
}
737737

738+
@Test // DATAES-857
739+
void shouldWriteEntityWithListOfGeoPoints() throws JSONException {
740+
741+
GeoPointListEntity entity = new GeoPointListEntity();
742+
entity.setId("42");
743+
List<GeoPoint> locations = Arrays.asList(new GeoPoint(12.34, 23.45), new GeoPoint(34.56, 45.67));
744+
entity.setLocations(locations);
745+
746+
String expected = "{\n" + //
747+
" \"id\": \"42\",\n" + //
748+
" \"locations\": [\n" + //
749+
" {\n" + //
750+
" \"lat\": 12.34,\n" + //
751+
" \"lon\": 23.45\n" + //
752+
" },\n" + //
753+
" {\n" + //
754+
" \"lat\": 34.56,\n" + //
755+
" \"lon\": 45.67\n" + //
756+
" }\n" + //
757+
" ]\n" + //
758+
"}"; //
759+
Document document = Document.create();
760+
761+
mappingElasticsearchConverter.write(entity, document);
762+
String json = document.toJson();
763+
764+
assertEquals(expected, json, false);
765+
}
766+
767+
@Test // DATAES-857
768+
void shouldReadEntityWithListOfGeoPoints() {
769+
770+
String json = "{\n" + //
771+
" \"id\": \"42\",\n" + //
772+
" \"locations\": [\n" + //
773+
" {\n" + //
774+
" \"lat\": 12.34,\n" + //
775+
" \"lon\": 23.45\n" + //
776+
" },\n" + //
777+
" {\n" + //
778+
" \"lat\": 34.56,\n" + //
779+
" \"lon\": 45.67\n" + //
780+
" }\n" + //
781+
" ]\n" + //
782+
"}"; //
783+
784+
Document document = Document.parse(json);
785+
786+
GeoPointListEntity entity = mappingElasticsearchConverter.read(GeoPointListEntity.class, document);
787+
788+
assertThat(entity.id).isEqualTo("42");
789+
assertThat(entity.locations).containsExactly(new GeoPoint(12.34, 23.45), new GeoPoint(34.56, 45.67));
790+
}
791+
738792
private String pointTemplate(String name, Point point) {
739793
return String.format(Locale.ENGLISH, "\"%s\":{\"lat\":%.1f,\"lon\":%.1f}", name, point.getX(), point.getY());
740794
}
@@ -956,4 +1010,10 @@ static class EntityWithListProperty {
9561010

9571011
private List<String> values;
9581012
}
1013+
1014+
@Data
1015+
static class GeoPointListEntity {
1016+
@Id String id;
1017+
List<GeoPoint> locations;
1018+
}
9591019
}

0 commit comments

Comments
 (0)