DATAMONGO-336 - Fixed potential NullPointerException in MongoTemplate.
The execution of MongoTemplate.geoNear(…) potentially caused NullPointerExceptions in case the actual query does not return any results. The wrapping return object returns null for the result list and general statistics which we didn't shield against.
This commit is contained in:
@@ -86,7 +86,6 @@ import org.springframework.util.Assert;
|
|||||||
import org.springframework.util.ResourceUtils;
|
import org.springframework.util.ResourceUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.mongodb.BasicDBList;
|
|
||||||
import com.mongodb.BasicDBObject;
|
import com.mongodb.BasicDBObject;
|
||||||
import com.mongodb.CommandResult;
|
import com.mongodb.CommandResult;
|
||||||
import com.mongodb.DB;
|
import com.mongodb.DB;
|
||||||
@@ -488,6 +487,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|||||||
return geoNear(near, entityClass, determineCollectionName(entityClass));
|
return geoNear(near, entityClass, determineCollectionName(entityClass));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String collectionName) {
|
public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String collectionName) {
|
||||||
|
|
||||||
if (near == null) {
|
if (near == null) {
|
||||||
@@ -503,7 +503,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|||||||
command.putAll(near.toDBObject());
|
command.putAll(near.toDBObject());
|
||||||
|
|
||||||
CommandResult commandResult = executeCommand(command);
|
CommandResult commandResult = executeCommand(command);
|
||||||
BasicDBList results = (BasicDBList) commandResult.get("results");
|
List<Object> results = (List<Object>) commandResult.get("results");
|
||||||
|
results = results == null ? Collections.emptyList() : results;
|
||||||
|
|
||||||
DbObjectCallback<GeoResult<T>> callback = new GeoNearResultDbObjectCallback<T>(new ReadDbObjectCallback<T>(
|
DbObjectCallback<GeoResult<T>> callback = new GeoNearResultDbObjectCallback<T>(new ReadDbObjectCallback<T>(
|
||||||
mongoConverter, entityClass), near.getMetric());
|
mongoConverter, entityClass), near.getMetric());
|
||||||
List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());
|
List<GeoResult<T>> result = new ArrayList<GeoResult<T>>(results.size());
|
||||||
@@ -512,7 +514,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
|||||||
result.add(callback.doWith((DBObject) element));
|
result.add(callback.doWith((DBObject) element));
|
||||||
}
|
}
|
||||||
|
|
||||||
double averageDistance = (Double) ((DBObject) commandResult.get("stats")).get("avgDistance");
|
DBObject stats = (DBObject) commandResult.get("stats");
|
||||||
|
double averageDistance = stats == null ? 0 : (Double) stats.get("avgDistance");
|
||||||
return new GeoResults<T>(result, new Distance(averageDistance, near.getMetric()));
|
return new GeoResults<T>(result, new Distance(averageDistance, near.getMetric()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ package org.springframework.data.mongodb.core.geo;
|
|||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.springframework.data.mongodb.core.query.Query.*;
|
|
||||||
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
import static org.springframework.data.mongodb.core.query.Criteria.*;
|
||||||
|
import static org.springframework.data.mongodb.core.query.Query.*;
|
||||||
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|||||||
Reference in New Issue
Block a user