Skip to content

Commit 4f031b7

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1453 - Fix GeoJson conversion when coordinates are Integers.
We now use Number instead of Double for reading "coordinates" from GeoJSON representations. Original pull request: #369.
1 parent a403807 commit 4f031b7

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -598,7 +598,7 @@ public GeoJsonPoint convert(Document source) {
598598
Assert.isTrue(ObjectUtils.nullSafeEquals(source.get("type"), "Point"),
599599
String.format("Cannot convert type '%s' to Point.", source.get("type")));
600600

601-
List<Double> dbl = (List<Double>) source.get("coordinates");
601+
List<Number> dbl = (List<Number>) source.get("coordinates");
602602
return new GeoJsonPoint(dbl.get(0).doubleValue(), dbl.get(1).doubleValue());
603603
}
604604
}
@@ -831,7 +831,7 @@ static List<Point> toListOfPoint(List listOfCoordinatePairs) {
831831

832832
Assert.isInstanceOf(List.class, point);
833833

834-
List<Double> coordinatesList = (List<Double>) point;
834+
List<Number> coordinatesList = (List<Number>) point;
835835

836836
points.add(new GeoJsonPoint(coordinatesList.get(0).doubleValue(), coordinatesList.get(1).doubleValue()));
837837
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonTests.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,26 +29,33 @@
2929
import org.junit.runner.RunWith;
3030
import org.springframework.beans.factory.annotation.Autowired;
3131
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.dao.DataAccessException;
3233
import org.springframework.data.annotation.Id;
3334
import org.springframework.data.annotation.PersistenceConstructor;
3435
import org.springframework.data.geo.GeoResults;
3536
import org.springframework.data.geo.Metric;
3637
import org.springframework.data.geo.Metrics;
3738
import org.springframework.data.geo.Point;
3839
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
40+
import org.springframework.data.mongodb.core.CollectionCallback;
3941
import org.springframework.data.mongodb.core.MongoTemplate;
4042
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
4143
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
4244
import org.springframework.data.mongodb.core.index.GeospatialIndex;
4345
import org.springframework.data.mongodb.core.mapping.Document;
4446
import org.springframework.data.mongodb.core.query.NearQuery;
4547
import org.springframework.data.mongodb.core.query.Query;
48+
import org.springframework.data.mongodb.test.util.BasicDbListBuilder;
4649
import org.springframework.test.context.ContextConfiguration;
4750
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4851

52+
import com.mongodb.BasicDBObject;
53+
import com.mongodb.DBCollection;
4954
import com.mongodb.Mongo;
5055
import com.mongodb.MongoClient;
56+
import com.mongodb.MongoException;
5157
import com.mongodb.WriteConcern;
58+
import com.mongodb.client.MongoCollection;
5259

5360
/**
5461
* @author Christoph Strobl
@@ -317,6 +324,67 @@ public void nearWithMinAndMaxDistance() {
317324
assertThat(venues.size(), is(2));
318325
}
319326

327+
/**
328+
* @see DATAMONGO-1453
329+
*/
330+
@Test
331+
public void shouldConvertPointRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
332+
333+
this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class),
334+
new CollectionCallback<Object>() {
335+
336+
@Override
337+
public Object doInCollection(MongoCollection<org.bson.Document> collection) throws MongoException, DataAccessException {
338+
339+
org.bson.Document pointRepresentation = new org.bson.Document();
340+
pointRepresentation.put("type", "Point");
341+
pointRepresentation.put("coordinates", Arrays.asList(0, 0));
342+
343+
org.bson.Document document = new org.bson.Document();
344+
document.append("_id", "datamongo-1453");
345+
document.append("geoJsonPoint", pointRepresentation);
346+
347+
collection.insertOne(document);
348+
return null;
349+
}
350+
});
351+
352+
assertThat(template.findOne(query(where("id").is("datamongo-1453")),
353+
DocumentWithPropertyUsingGeoJsonType.class).geoJsonPoint, is(equalTo(new GeoJsonPoint(0D, 0D))));
354+
}
355+
356+
/**
357+
* @see DATAMONGO-1453
358+
*/
359+
@Test
360+
public void shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
361+
362+
this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class),
363+
new CollectionCallback<Object>() {
364+
365+
@Override
366+
public Object doInCollection(MongoCollection<org.bson.Document> collection) throws MongoException, DataAccessException {
367+
368+
org.bson.Document lineStringRepresentation = new org.bson.Document();
369+
lineStringRepresentation.put("type", "LineString");
370+
lineStringRepresentation.put("coordinates",
371+
Arrays.asList(Arrays.asList(0, 0), Arrays.asList(1, 1)));
372+
373+
org.bson.Document document = new org.bson.Document();
374+
document.append("_id", "datamongo-1453");
375+
document.append("geoJsonLineString", lineStringRepresentation);
376+
377+
collection.insertOne(document);
378+
return null;
379+
}
380+
});
381+
382+
assertThat(
383+
template.findOne(query(where("id").is("datamongo-1453")),
384+
DocumentWithPropertyUsingGeoJsonType.class).geoJsonLineString,
385+
is(equalTo(new GeoJsonLineString(new Point(0D, 0D), new Point(1, 1)))));
386+
}
387+
320388
private void addVenues() {
321389

322390
template.insert(new Venue2DSphere("Penn Station", -73.99408, 40.75057));

0 commit comments

Comments
 (0)