Skip to content

Commit 95985ff

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1588 - Fix derived finder not accepting subclass of parameter type.
We now allow using sub types as arguments for derived queries. This makes it possible to use eg. a GeoJsonPoint for querying while the declared property type in the domain object remains a regular (legacy) Point. Original pull request: #435.
1 parent 6c6ac6d commit 95985ff

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2015 the original author or authors.
2+
* Copyright 2010-2017 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.
@@ -47,6 +47,7 @@
4747
import org.springframework.data.repository.query.parser.Part.Type;
4848
import org.springframework.data.repository.query.parser.PartTree;
4949
import org.springframework.util.Assert;
50+
import org.springframework.util.ClassUtils;
5051

5152
/**
5253
* Custom query creator to create Mongo criterias.
@@ -367,8 +368,10 @@ private String toRegexOptions(Part part) {
367368
*/
368369
@SuppressWarnings("unchecked")
369370
private <T> T nextAs(Iterator<Object> iterator, Class<T> type) {
371+
370372
Object parameter = iterator.next();
371-
if (parameter.getClass().isAssignableFrom(type)) {
373+
374+
if (ClassUtils.isAssignable(type, parameter.getClass())) {
372375
return (T) parameter;
373376
}
374377

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.data.geo.Point;
5252
import org.springframework.data.geo.Polygon;
5353
import org.springframework.data.mongodb.core.MongoOperations;
54+
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
5455
import org.springframework.data.mongodb.core.query.BasicQuery;
5556
import org.springframework.data.mongodb.repository.Person.Sex;
5657
import org.springframework.data.mongodb.repository.SampleEvaluationContextExtension.SampleSecurityContextHolder;
@@ -272,6 +273,18 @@ public void findsPeopleByLocationNear() {
272273
assertThat(result, hasItem(dave));
273274
}
274275

276+
@Test // DATAMONGO-1588
277+
public void findsPeopleByLocationNearUsingGeoJsonType() {
278+
279+
GeoJsonPoint point = new GeoJsonPoint(-73.99171, 40.738868);
280+
dave.setLocation(point);
281+
repository.save(dave);
282+
283+
List<Person> result = repository.findByLocationNear(point);
284+
assertThat(result.size(), is(1));
285+
assertThat(result, hasItem(dave));
286+
}
287+
275288
@Test
276289
public void findsPeopleByLocationWithinCircle() {
277290
Point point = new Point(-73.99171, 40.738868);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
4545
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
4646
import org.springframework.data.mongodb.core.convert.MongoConverter;
47+
import org.springframework.data.mongodb.core.geo.GeoJsonLineString;
48+
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
4749
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
4850
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
4951
import org.springframework.data.mongodb.core.mapping.DBRef;
@@ -669,6 +671,35 @@ public void bindsNullValueToContainsClause() {
669671
assertThat(query, is(query(where("emailAddresses").in((Object) null))));
670672
}
671673

674+
/**
675+
* @see DATAMONGO-1588
676+
*/
677+
@Test // DATAMONGO-1588
678+
public void queryShouldAcceptSubclassOfDeclaredArgument() {
679+
680+
PartTree tree = new PartTree("findByLocationNear", User.class);
681+
ConvertingParameterAccessor accessor = getAccessor(converter, new GeoJsonPoint(-74.044502D, 40.689247D));
682+
683+
Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
684+
assertThat(query.getQueryObject().containsField("location"), is(true));
685+
}
686+
687+
/**
688+
* @see DATAMONGO-1588
689+
*/
690+
@Test
691+
public void queryShouldThrowExceptionWhenArgumentDoesNotMatchDeclaration() {
692+
693+
expection.expect(IllegalArgumentException.class);
694+
expection.expectMessage("Expected parameter type of " + Point.class);
695+
696+
PartTree tree = new PartTree("findByLocationNear", User.class);
697+
ConvertingParameterAccessor accessor = getAccessor(converter,
698+
new GeoJsonLineString(new Point(-74.044502D, 40.689247D), new Point(-73.997330D, 40.730824D)));
699+
700+
new MongoQueryCreator(tree, accessor, context).createQuery();
701+
}
702+
672703
interface PersonRepository extends Repository<Person, Long> {
673704

674705
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);
@@ -687,6 +718,8 @@ class User {
687718
Address address;
688719

689720
Address2dSphere address2dSphere;
721+
722+
Point location;
690723
}
691724

692725
static class Address {

0 commit comments

Comments
 (0)