mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-05-31 09:12:11 +00:00
DATAES-857 - Registered simple types are not read from list.
Original PR: #478
This commit is contained in:
parent
407da040ab
commit
407c8c6c17
@ -316,14 +316,17 @@ public class MappingElasticsearchConverter
|
||||
}
|
||||
|
||||
Collection<Object> target = createCollectionForValue(targetType, source.size());
|
||||
TypeInformation<?> componentType = targetType.getComponentType();
|
||||
|
||||
for (Object value : source) {
|
||||
|
||||
if (value == null) {
|
||||
target.add(null);
|
||||
} else if (componentType != null && !ClassTypeInformation.OBJECT.equals(componentType)
|
||||
&& isSimpleType(componentType.getType())) {
|
||||
target.add(readSimpleValue(value, componentType));
|
||||
} else if (isSimpleType(value)) {
|
||||
target.add(
|
||||
readSimpleValue(value, targetType.getComponentType() != null ? targetType.getComponentType() : targetType));
|
||||
target.add(readSimpleValue(value, componentType != null ? componentType : targetType));
|
||||
} else {
|
||||
|
||||
if (value instanceof List) {
|
||||
|
@ -17,6 +17,8 @@ package org.springframework.data.elasticsearch.core.geo;
|
||||
|
||||
import org.springframework.data.geo.Point;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
|
||||
@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
|
||||
public String toString() {
|
||||
return "GeoPoint{" +
|
||||
|
@ -735,6 +735,60 @@ public class MappingElasticsearchConverterUnitTests {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
@Data
|
||||
static class GeoPointListEntity {
|
||||
@Id String id;
|
||||
List<GeoPoint> locations;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user