From 218f32e5522e10b7bdd47a7fe6675fb0e2ab2d53 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 5 Jun 2015 06:49:03 +0200 Subject: [PATCH] DATAMONGO-1229 - Fixed application of ignore case flag on nested properties. Previously we tried to apply the ignore case settings found in the PartTree to the root PropertyPath we handle in MongoQueryCreator.create(). This is now changed to work on the leaf property of the PropertyPath. --- .../repository/query/MongoQueryCreator.java | 15 ++++++++++----- .../query/MongoQueryCreatorUnitTests.java | 12 ++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java index c2d2ff75a..6a34d1d13 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java @@ -29,6 +29,7 @@ import org.springframework.data.geo.Distance; import org.springframework.data.geo.Metrics; import org.springframework.data.geo.Point; import org.springframework.data.geo.Shape; +import org.springframework.data.mapping.PropertyPath; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.PersistentPropertyPath; import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; @@ -276,19 +277,23 @@ class MongoQueryCreator extends AbstractQueryCreator { private Criteria createLikeRegexCriteriaOrThrow(Part part, MongoPersistentProperty property, Criteria criteria, PotentiallyConvertingIterator parameters, boolean shouldNegateExpression) { + PropertyPath path = part.getProperty().getLeafProperty(); + switch (part.shouldIgnoreCase()) { case ALWAYS: - if (part.getProperty().getType() != String.class) { - throw new IllegalArgumentException(String.format("part %s must be of type String but was %s", - part.getProperty(), part.getType())); + if (path.getType() != String.class) { + throw new IllegalArgumentException( + String.format("Part %s must be of type String but was %s", path, path.getType())); } // fall-through case WHEN_POSSIBLE: + if (shouldNegateExpression) { criteria = criteria.not(); } + return addAppropriateLikeRegexTo(criteria, part, parameters.nextConverted(property).toString()); case NEVER: @@ -365,8 +370,8 @@ class MongoQueryCreator extends AbstractQueryCreator { return (T) parameter; } - throw new IllegalArgumentException(String.format("Expected parameter type of %s but got %s!", type, - parameter.getClass())); + throw new IllegalArgumentException( + String.format("Expected parameter type of %s but got %s!", type, parameter.getClass())); } private Object[] nextAsArray(PotentiallyConvertingIterator iterator, MongoPersistentProperty property) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java index 06873b78c..dfade4bd3 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java @@ -553,6 +553,18 @@ public class MongoQueryCreatorUnitTests { assertThat(query, is(query(where("address.geo").near(point).minDistance(10D).maxDistance(20D)))); } + /** + * @see DATAMONGO-1229 + */ + @Test + public void appliesIgnoreCaseToLeafProperty() { + + PartTree tree = new PartTree("findByAddressStreetIgnoreCase", User.class); + ConvertingParameterAccessor accessor = getAccessor(converter, "Street"); + + assertThat(new MongoQueryCreator(tree, accessor, context).createQuery(), is(notNullValue())); + } + interface PersonRepository extends Repository { List findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);