mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-05 18:22:23 +00:00
DATAES-857 - Registered simple types are not read from list.
Original PR: #478 (cherry picked from commit 407c8c6c17cf13dffcf0c577fe7ea47bd6f96200)
This commit is contained in:
parent
ff999959a8
commit
8e765cf07c
@ -316,14 +316,17 @@ public class MappingElasticsearchConverter
|
|||||||
}
|
}
|
||||||
|
|
||||||
Collection<Object> target = createCollectionForValue(targetType, source.size());
|
Collection<Object> target = createCollectionForValue(targetType, source.size());
|
||||||
|
TypeInformation<?> componentType = targetType.getComponentType();
|
||||||
|
|
||||||
for (Object value : source) {
|
for (Object value : source) {
|
||||||
|
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
target.add(null);
|
target.add(null);
|
||||||
|
} else if (componentType != null && !ClassTypeInformation.OBJECT.equals(componentType)
|
||||||
|
&& isSimpleType(componentType.getType())) {
|
||||||
|
target.add(readSimpleValue(value, componentType));
|
||||||
} else if (isSimpleType(value)) {
|
} else if (isSimpleType(value)) {
|
||||||
target.add(
|
target.add(readSimpleValue(value, componentType != null ? componentType : targetType));
|
||||||
readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType));
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (value instanceof List) {
|
if (value instanceof List) {
|
||||||
|
@ -17,6 +17,8 @@ package org.springframework.data.elasticsearch.core.geo;
|
|||||||
|
|
||||||
import org.springframework.data.geo.Point;
|
import org.springframework.data.geo.Point;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* geo-location used for #{@link org.springframework.data.elasticsearch.core.query.Criteria}.
|
* geo-location used for #{@link org.springframework.data.elasticsearch.core.query.Criteria}.
|
||||||
*
|
*
|
||||||
@ -60,6 +62,20 @@ public class GeoPoint {
|
|||||||
return new Point(point.getLat(), point.getLon());
|
return new Point(point.getLat(), point.getLon());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
GeoPoint geoPoint = (GeoPoint) o;
|
||||||
|
return Double.compare(geoPoint.lat, lat) == 0 &&
|
||||||
|
Double.compare(geoPoint.lon, lon) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(lat, lon);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "GeoPoint{" +
|
return "GeoPoint{" +
|
||||||
|
@ -735,6 +735,60 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertEquals(expected, json, false);
|
assertEquals(expected, json, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // DATAES-857
|
||||||
|
void shouldWriteEntityWithListOfGeoPoints() throws JSONException {
|
||||||
|
|
||||||
|
GeoPointListEntity entity = new GeoPointListEntity();
|
||||||
|
entity.setId("42");
|
||||||
|
List<GeoPoint> locations = Arrays.asList(new GeoPoint(12.34, 23.45), new GeoPoint(34.56, 45.67));
|
||||||
|
entity.setLocations(locations);
|
||||||
|
|
||||||
|
String expected = "{\n" + //
|
||||||
|
" \"id\": \"42\",\n" + //
|
||||||
|
" \"locations\": [\n" + //
|
||||||
|
" {\n" + //
|
||||||
|
" \"lat\": 12.34,\n" + //
|
||||||
|
" \"lon\": 23.45\n" + //
|
||||||
|
" },\n" + //
|
||||||
|
" {\n" + //
|
||||||
|
" \"lat\": 34.56,\n" + //
|
||||||
|
" \"lon\": 45.67\n" + //
|
||||||
|
" }\n" + //
|
||||||
|
" ]\n" + //
|
||||||
|
"}"; //
|
||||||
|
Document document = Document.create();
|
||||||
|
|
||||||
|
mappingElasticsearchConverter.write(entity, document);
|
||||||
|
String json = document.toJson();
|
||||||
|
|
||||||
|
assertEquals(expected, json, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test // DATAES-857
|
||||||
|
void shouldReadEntityWithListOfGeoPoints() {
|
||||||
|
|
||||||
|
String json = "{\n" + //
|
||||||
|
" \"id\": \"42\",\n" + //
|
||||||
|
" \"locations\": [\n" + //
|
||||||
|
" {\n" + //
|
||||||
|
" \"lat\": 12.34,\n" + //
|
||||||
|
" \"lon\": 23.45\n" + //
|
||||||
|
" },\n" + //
|
||||||
|
" {\n" + //
|
||||||
|
" \"lat\": 34.56,\n" + //
|
||||||
|
" \"lon\": 45.67\n" + //
|
||||||
|
" }\n" + //
|
||||||
|
" ]\n" + //
|
||||||
|
"}"; //
|
||||||
|
|
||||||
|
Document document = Document.parse(json);
|
||||||
|
|
||||||
|
GeoPointListEntity entity = mappingElasticsearchConverter.read(GeoPointListEntity.class, document);
|
||||||
|
|
||||||
|
assertThat(entity.id).isEqualTo("42");
|
||||||
|
assertThat(entity.locations).containsExactly(new GeoPoint(12.34, 23.45), new GeoPoint(34.56, 45.67));
|
||||||
|
}
|
||||||
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
@ -956,4 +1010,10 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
|
|
||||||
private List<String> values;
|
private List<String> values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
static class GeoPointListEntity {
|
||||||
|
@Id String id;
|
||||||
|
List<GeoPoint> locations;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user