diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java index 77ecfa1ff..86901f652 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ import org.springframework.data.mongodb.core.geo.GeoJsonPolygon; import org.springframework.data.mongodb.core.geo.Sphere; import org.springframework.data.mongodb.core.query.GeoCommand; import org.springframework.util.Assert; +import org.springframework.util.NumberUtils; import org.springframework.util.ObjectUtils; import com.mongodb.BasicDBList; @@ -121,10 +122,7 @@ abstract class GeoConverters { return DocumentToGeoJsonPointConverter.INSTANCE.convert(source); } - Number x = (Number) source.get("x"); - Number y = (Number) source.get("y"); - - return new Point(x.doubleValue(), y.doubleValue()); + return new Point(toPrimitiveDoubleValue(source.get("x")), toPrimitiveDoubleValue(source.get("y"))); } } @@ -258,10 +256,12 @@ abstract class GeoConverters { } Document center = (Document) source.get("center"); - Number radiusNumber = (Number) source.get("radius"); - Double radius = radiusNumber.doubleValue(); + Number radius = (Number) source.get("radius"); + + Assert.notNull(center, "Center must not be null!"); + Assert.notNull(radius, "Radius must not be null!"); - Distance distance = new Distance(radius); + Distance distance = new Distance(toPrimitiveDoubleValue(radius)); if (source.containsKey("metric")) { @@ -271,9 +271,6 @@ abstract class GeoConverters { distance = distance.in(Metrics.valueOf(metricString)); } - Assert.notNull(center, "Center must not be null!"); - Assert.notNull(radius, "Radius must not be null!"); - return new Circle(DocumentToPointConverter.INSTANCE.convert(center), distance); } } @@ -330,11 +327,12 @@ abstract class GeoConverters { } Document center = (Document) source.get("center"); - Number radiusNumber = (Number) source.get("radius"); - Double radius = radiusNumber.doubleValue(); + Number radius = (Number) source.get("radius"); + Assert.notNull(center, "Center must not be null!"); + Assert.notNull(radius, "Radius must not be null!"); - Distance distance = new Distance(radius); + Distance distance = new Distance(toPrimitiveDoubleValue(radius)); if (source.containsKey("metric")) { @@ -344,9 +342,6 @@ abstract class GeoConverters { distance = distance.in(Metrics.valueOf(metricString)); } - Assert.notNull(center, "Center must not be null!"); - Assert.notNull(radius, "Radius must not be null!"); - return new Sphere(DocumentToPointConverter.INSTANCE.convert(center), distance); } } @@ -606,7 +601,7 @@ abstract class GeoConverters { String.format("Cannot convert type '%s' to Point.", source.get("type"))); List dbl = (List) source.get("coordinates"); - return new GeoJsonPoint(dbl.get(0).doubleValue(), dbl.get(1).doubleValue()); + return new GeoJsonPoint(toPrimitiveDoubleValue(dbl.get(0)), toPrimitiveDoubleValue(dbl.get(1))); } } @@ -840,7 +835,8 @@ abstract class GeoConverters { List coordinatesList = (List) point; - points.add(new GeoJsonPoint(coordinatesList.get(0).doubleValue(), coordinatesList.get(1).doubleValue())); + points.add(new GeoJsonPoint(toPrimitiveDoubleValue(coordinatesList.get(0)), + toPrimitiveDoubleValue(coordinatesList.get(1)))); } return points; } @@ -855,4 +851,10 @@ abstract class GeoConverters { static GeoJsonPolygon toGeoJsonPolygon(List dbList) { return new GeoJsonPolygon(toListOfPoint((List) dbList.get(0))); } + + private static double toPrimitiveDoubleValue(Object value) { + + Assert.isInstanceOf(Number.class, value, "Argument must be a Number."); + return NumberUtils.convertNumberToTargetClass((Number) value, Double.class).doubleValue(); + } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java index 06cb94843..e5f11927b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/GeoConvertersUnitTests.java @@ -48,6 +48,7 @@ import org.springframework.data.mongodb.core.query.GeoCommand; * * @author Thomas Darimont * @author Oliver Gierke + * @author Christoph Strobl * @since 1.5 */ public class GeoConvertersUnitTests { @@ -153,4 +154,32 @@ public class GeoConvertersUnitTests { assertThat(boxObject, is((Object) Arrays.asList(GeoConverters.toList(box.getFirst()), GeoConverters.toList(box.getSecond())))); } + + @Test // DATAMONGO-1607 + public void convertsPointCorrectlyWhenUsingNonDoubleForCoordinates() { + + assertThat(DocumentToPointConverter.INSTANCE.convert(new Document().append("x", 1L).append("y", 2L)), + is(new Point(1, 2))); + } + + @Test // DATAMONGO-1607 + public void convertsCircleCorrectlyWhenUsingNonDoubleForCoordinates() { + + Document circle = new Document(); + circle.put("center", new Document().append("x", 1).append("y", 2)); + circle.put("radius", 3L); + + assertThat(DocumentToCircleConverter.INSTANCE.convert(circle), is(new Circle(new Point(1, 2), new Distance(3)))); + } + + @Test // DATAMONGO-1607 + public void convertsSphereCorrectlyWhenUsingNonDoubleForCoordinates() { + + Document sphere = new Document(); + sphere.put("center", new Document().append("x", 1).append("y", 2)); + sphere.put("radius", 3L); + + assertThat(DocumentToSphereConverter.INSTANCE.convert(sphere), is(new Sphere(new Point(1, 2), new Distance(3)))); + } + }