Skip to content

Commit 35fa922

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 8f7ea3a commit 35fa922

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-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-2016 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.
@@ -369,8 +370,10 @@ private String toRegexOptions(Part part) {
369370
*/
370371
@SuppressWarnings("unchecked")
371372
private <T> T nextAs(Iterator<Object> iterator, Class<T> type) {
373+
372374
Object parameter = iterator.next();
373-
if (parameter.getClass().isAssignableFrom(type)) {
375+
376+
if (ClassUtils.isAssignable(type, parameter.getClass())) {
374377
return (T) parameter;
375378
}
376379

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;
@@ -273,6 +274,18 @@ public void findsPeopleByLocationNear() {
273274
assertThat(result, hasItem(dave));
274275
}
275276

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

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

Lines changed: 27 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;
@@ -604,6 +606,29 @@ public void notLikeShouldBeTreatedCorrectlyWhenUsedWithWildcardOnly() {
604606
assertThat(query.getQueryObject(), is(query(where("username").not().regex(".*")).getQueryObject()));
605607
}
606608

609+
@Test // DATAMONGO-1588
610+
public void queryShouldAcceptSubclassOfDeclaredArgument() {
611+
612+
PartTree tree = new PartTree("findByLocationNear", User.class);
613+
ConvertingParameterAccessor accessor = getAccessor(converter, new GeoJsonPoint(-74.044502D, 40.689247D));
614+
615+
Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
616+
assertThat(query.getQueryObject().containsField("location"), is(true));
617+
}
618+
619+
@Test // DATAMONGO-1588
620+
public void queryShouldThrowExceptionWhenArgumentDoesNotMatchDeclaration() {
621+
622+
expection.expect(IllegalArgumentException.class);
623+
expection.expectMessage("Expected parameter type of " + Point.class);
624+
625+
PartTree tree = new PartTree("findByLocationNear", User.class);
626+
ConvertingParameterAccessor accessor = getAccessor(converter,
627+
new GeoJsonLineString(new Point(-74.044502D, 40.689247D), new Point(-73.997330D, 40.730824D)));
628+
629+
new MongoQueryCreator(tree, accessor, context).createQuery();
630+
}
631+
607632
interface PersonRepository extends Repository<Person, Long> {
608633

609634
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);
@@ -622,6 +647,8 @@ class User {
622647
Address address;
623648

624649
Address2dSphere address2dSphere;
650+
651+
Point location;
625652
}
626653

627654
static class Address {

0 commit comments

Comments
 (0)