Skip to content

Commit d35d38f

Browse files
committed
GeoJson types can be lowercase in Elasticsearch.
Original Pull Request #1657 Closes #1655 (cherry picked from commit 159520d)
1 parent 2883280 commit d35d38f

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

Diff for: src/main/java/org/springframework/data/elasticsearch/core/convert/GeoConverters.java

+16-15
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,19 @@ public GeoJson<? extends Iterable<?>> convert(Map<String, Object> source) {
172172
String type = GeoConverters.getGeoJsonType(source);
173173

174174
switch (type) {
175-
case GeoJsonPoint.TYPE:
175+
case "point":
176176
return MapToGeoJsonPointConverter.INSTANCE.convert(source);
177-
case GeoJsonMultiPoint.TYPE:
177+
case "multipoint":
178178
return MapToGeoJsonMultiPointConverter.INSTANCE.convert(source);
179-
case GeoJsonLineString.TYPE:
179+
case "linestring":
180180
return MapToGeoJsonLineStringConverter.INSTANCE.convert(source);
181-
case GeoJsonMultiLineString.TYPE:
181+
case "multilinestring":
182182
return MapToGeoJsonMultiLineStringConverter.INSTANCE.convert(source);
183-
case GeoJsonPolygon.TYPE:
183+
case "polygon":
184184
return MapToGeoJsonPolygonConverter.INSTANCE.convert(source);
185-
case GeoJsonMultiPolygon.TYPE:
185+
case "multipolygon":
186186
return MapToGeoJsonMultiPolygonConverter.INSTANCE.convert(source);
187-
case GeoJsonGeometryCollection.TYPE:
187+
case "geometrycollection":
188188
return MapToGeoJsonGeometryCollectionConverter.INSTANCE.convert(source);
189189
default:
190190
throw new IllegalArgumentException("unknown GeoJson type " + type);
@@ -217,7 +217,7 @@ public enum MapToGeoJsonPointConverter implements Converter<Map<String, Object>,
217217
public GeoJsonPoint convert(Map<String, Object> source) {
218218

219219
String type = GeoConverters.getGeoJsonType(source);
220-
Assert.isTrue(type.equals(GeoJsonPoint.TYPE), "does not contain a type 'Point'");
220+
Assert.isTrue(type.equalsIgnoreCase(GeoJsonPoint.TYPE), "does not contain a type 'Point'");
221221

222222
Object coordinates = source.get("coordinates");
223223
Assert.notNull(coordinates, "Document to convert does not contain coordinates");
@@ -255,7 +255,7 @@ public enum MapToGeoJsonMultiPointConverter implements Converter<Map<String, Obj
255255
public GeoJsonMultiPoint convert(Map<String, Object> source) {
256256

257257
String type = GeoConverters.getGeoJsonType(source);
258-
Assert.isTrue(type.equals(GeoJsonMultiPoint.TYPE), "does not contain a type 'MultiPoint'");
258+
Assert.isTrue(type.equalsIgnoreCase(GeoJsonMultiPoint.TYPE), "does not contain a type 'MultiPoint'");
259259
Object coordinates = source.get("coordinates");
260260
Assert.notNull(coordinates, "Document to convert does not contain coordinates");
261261
Assert.isTrue(coordinates instanceof List, "coordinates must be a List");
@@ -290,7 +290,7 @@ public enum MapToGeoJsonLineStringConverter implements Converter<Map<String, Obj
290290
public GeoJsonLineString convert(Map<String, Object> source) {
291291

292292
String type = GeoConverters.getGeoJsonType(source);
293-
Assert.isTrue(type.equals(GeoJsonLineString.TYPE), "does not contain a type 'LineString'");
293+
Assert.isTrue(type.equalsIgnoreCase(GeoJsonLineString.TYPE), "does not contain a type 'LineString'");
294294
Object coordinates = source.get("coordinates");
295295
Assert.notNull(coordinates, "Document to convert does not contain coordinates");
296296
Assert.isTrue(coordinates instanceof List, "coordinates must be a List");
@@ -322,7 +322,7 @@ public enum MapToGeoJsonMultiLineStringConverter implements Converter<Map<String
322322
public GeoJsonMultiLineString convert(Map<String, Object> source) {
323323

324324
String type = GeoConverters.getGeoJsonType(source);
325-
Assert.isTrue(type.equals(GeoJsonMultiLineString.TYPE), "does not contain a type 'MultiLineString'");
325+
Assert.isTrue(type.equalsIgnoreCase(GeoJsonMultiLineString.TYPE), "does not contain a type 'MultiLineString'");
326326
List<GeoJsonLineString> lines = geoJsonLineStringsFromMap(source);
327327
return GeoJsonMultiLineString.of(lines);
328328
}
@@ -350,7 +350,7 @@ public enum MapToGeoJsonPolygonConverter implements Converter<Map<String, Object
350350
public GeoJsonPolygon convert(Map<String, Object> source) {
351351

352352
String type = GeoConverters.getGeoJsonType(source);
353-
Assert.isTrue(type.equals(GeoJsonPolygon.TYPE), "does not contain a type 'Polygon'");
353+
Assert.isTrue(type.equalsIgnoreCase(GeoJsonPolygon.TYPE), "does not contain a type 'Polygon'");
354354
List<GeoJsonLineString> lines = geoJsonLineStringsFromMap(source);
355355
Assert.isTrue(lines.size() > 0, "no linestrings defined in polygon");
356356
GeoJsonPolygon geoJsonPolygon = GeoJsonPolygon.of(lines.get(0));
@@ -394,7 +394,7 @@ public enum MapToGeoJsonMultiPolygonConverter implements Converter<Map<String, O
394394
public GeoJsonMultiPolygon convert(Map<String, Object> source) {
395395

396396
String type = GeoConverters.getGeoJsonType(source);
397-
Assert.isTrue(type.equals(GeoJsonMultiPolygon.TYPE), "does not contain a type 'MultiPolygon'");
397+
Assert.isTrue(type.equalsIgnoreCase(GeoJsonMultiPolygon.TYPE), "does not contain a type 'MultiPolygon'");
398398
Object coordinates = source.get("coordinates");
399399
Assert.notNull(coordinates, "Document to convert does not contain coordinates");
400400
Assert.isTrue(coordinates instanceof List, "coordinates must be a List");
@@ -441,7 +441,8 @@ public enum MapToGeoJsonGeometryCollectionConverter
441441
public GeoJsonGeometryCollection convert(Map<String, Object> source) {
442442

443443
String type = GeoConverters.getGeoJsonType(source);
444-
Assert.isTrue(type.equals(GeoJsonGeometryCollection.TYPE), "does not contain a type 'GeometryCollection'");
444+
Assert.isTrue(type.equalsIgnoreCase(GeoJsonGeometryCollection.TYPE),
445+
"does not contain a type 'GeometryCollection'");
445446
Object geometries = source.get("geometries");
446447
Assert.notNull(geometries, "Document to convert does not contain geometries");
447448
Assert.isTrue(geometries instanceof List, "geometries must be a List");
@@ -461,7 +462,7 @@ private static String getGeoJsonType(Map<String, Object> source) {
461462
Assert.notNull(type, "Document to convert does not contain a type");
462463
Assert.isTrue(type instanceof String, "type must be a String");
463464

464-
return type.toString();
465+
return type.toString().toLowerCase();
465466
}
466467

467468
private static List<Double> toCoordinates(Point point) {

Diff for: src/test/java/org/springframework/data/elasticsearch/core/convert/GeoConvertersUnitTests.java

+19-10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class GeoJsonTests {
5353
@DisplayName("GeoJsonPoint")
5454
class GeoJsonPointUnitTests {
5555

56+
// NOTE: the test converting from a map contains the type names in lowercase, that might be returned from
57+
// Elasticsearch
58+
5659
@Test // DATAES-930
5760
@DisplayName("should be converted to a Map")
5861
void shouldBeConvertedToAMap() throws JSONException {
@@ -75,7 +78,7 @@ void shouldBeConvertedFromAMap() {
7578

7679
// make sure we can read int values as well
7780
String json = "{\n" + //
78-
" \"type\": \"Point\",\n" + //
81+
" \"type\": \"point\",\n" + //
7982
" \"coordinates\": [12, 34.0]\n" + //
8083
"}"; //
8184

@@ -117,8 +120,14 @@ void shouldBeConvertedToAMap() throws JSONException {
117120
void shouldBeConvertedFromAMap() {
118121

119122
// make sure we can read int values as well
120-
String json = "{\n" + " \"type\": \"MultiPoint\",\n" + " \"coordinates\": [\n" + " [12.0, 34],\n"
121-
+ " [56, 78.0]\n" + " ]\n" + "}\n";
123+
String json = "{\n" + //
124+
" \"type\": \"multipoint\",\n" //
125+
+ " \"coordinates\": [\n" + //
126+
" [12.0, 34],\n" + //
127+
" [56, 78.0]\n" + //
128+
" ]\n" + //
129+
"}\n"; //
130+
122131
Document document = Document.parse(json);
123132

124133
GeoJsonMultiPoint expected = GeoJsonMultiPoint.of(new Point(12, 34), new Point(56, 78));
@@ -158,7 +167,7 @@ void shouldBeConvertedFromAMap() {
158167

159168
// make sure we can read int values as well
160169
String json = "{\n" + //
161-
" \"type\": \"LineString\",\n" + //
170+
" \"type\": \"linestring\",\n" + //
162171
" \"coordinates\": [\n" + //
163172
" [12.0, 34],\n" + //
164173
" [56, 78.0]\n" //
@@ -205,7 +214,7 @@ void shouldBeConvertedToAMap() throws JSONException {
205214
void shouldBeConvertedFromAMap() {
206215
// make sure we can read int values as well
207216
String json = "{\n" + //
208-
" \"type\": \"MultiLineString\",\n" + //
217+
" \"type\": \"multilinestring\",\n" + //
209218
" \"coordinates\": [\n" + //
210219
" [[12, 34.0], [56.0, 78]],\n" + //
211220
" [[90.0, 12], [34, 56.0]]\n" + //
@@ -256,7 +265,7 @@ void shouldBeConvertedToAMap() throws JSONException {
256265
void shouldBeConvertedFromAMap() {
257266

258267
String json = "{\n" + //
259-
" \"type\": \"Polygon\",\n" + //
268+
" \"type\": \"polygon\",\n" + //
260269
" \"coordinates\": [\n" + //
261270
" [[12, 34.0], [56.0, 78], [90, 12.0], [12, 34.0]],\n" + //
262271
" [[56.0, 78], [90, 12.0], [34.0, 56], [56.0, 78]]\n" + //
@@ -308,7 +317,7 @@ void shouldBeConvertedToAMap() throws JSONException {
308317
void shouldBeConvertedFromAMap() {
309318

310319
String json = "{\n" + //
311-
" \"type\": \"MultiPolygon\",\n" + //
320+
" \"type\": \"multipolygon\",\n" + //
312321
" \"coordinates\": [\n" + //
313322
" [[[12, 34.0], [56.0, 78], [90, 12.0], [12, 34.0]]],\n" + //
314323
" [[[56, 78.0], [90, 12.0], [34.0, 56], [56, 78.0]]]\n" + //
@@ -369,14 +378,14 @@ void shouldBeConvertedToAMap() throws JSONException {
369378
void shouldBeConvertedFromAMap() {
370379

371380
String json = "{\n" + //
372-
" \"type\": \"GeometryCollection\",\n" + //
381+
" \"type\": \"geometrycollection\",\n" + //
373382
" \"geometries\": [\n" + //
374383
" {\n" + //
375-
" \"type\": \"Point\",\n" + //
384+
" \"type\": \"point\",\n" + //
376385
" \"coordinates\": [12.0, 34.0]\n" + //
377386
" },\n" + //
378387
" {\n" + //
379-
" \"type\": \"Polygon\",\n" + //
388+
" \"type\": \"polygon\",\n" + //
380389
" \"coordinates\": [\n" + //
381390
" [[12.0, 34.0], [56.0, 78.0], [90.0, 12.0], [12.0, 34.0]]\n" + //
382391
" ]\n" + //

0 commit comments

Comments
 (0)