Skip to content

Commit 0c0f47f

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2003 - Fix derived query using regex pattern with options.
We now consider regex pattern options when using the pattern as a derived finder argument. Original pull request: #570.
1 parent 18313db commit 0c0f47f

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.Iterator;
2323
import java.util.Optional;
24+
import java.util.regex.Pattern;
2425

2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
@@ -206,7 +207,9 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
206207
case NOT_CONTAINING:
207208
return createContainingCriteria(part, property, criteria.not(), parameters);
208209
case REGEX:
209-
return criteria.regex(parameters.next().toString());
210+
211+
Object param = parameters.next();
212+
return param instanceof Pattern ? criteria.regex((Pattern) param) : criteria.regex(param.toString());
210213
case EXISTS:
211214
return criteria.exists((Boolean) parameters.next());
212215
case TRUE:

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.Optional;
3030
import java.util.UUID;
31+
import java.util.regex.Pattern;
3132
import java.util.stream.Collectors;
3233
import java.util.stream.Stream;
3334

@@ -1216,4 +1217,18 @@ public void findWithSortOverwritesAnnotatedSort() {
12161217
assertThat(repository.findByAgeGreaterThan(40, Sort.by(Direction.ASC, "age"))).containsExactly(leroi, dave, boyd,
12171218
carter);
12181219
}
1220+
1221+
@Test // DATAMONGO-2003
1222+
public void findByRegexWithPattern() {
1223+
assertThat(repository.findByFirstnameRegex(Pattern.compile(alicia.getFirstname()))).hasSize(1);
1224+
}
1225+
1226+
@Test // DATAMONGO-2003
1227+
public void findByRegexWithPatternAndOptions() {
1228+
1229+
String fn = alicia.getFirstname().toUpperCase();
1230+
1231+
assertThat(repository.findByFirstnameRegex(Pattern.compile(fn))).hasSize(0);
1232+
assertThat(repository.findByFirstnameRegex(Pattern.compile(fn, Pattern.CASE_INSENSITIVE))).hasSize(1);
1233+
}
12191234
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Optional;
2222
import java.util.UUID;
23+
import java.util.regex.Pattern;
2324
import java.util.stream.Stream;
2425

2526
import org.springframework.data.domain.Page;
@@ -353,4 +354,6 @@ Page<Person> findByCustomQueryLastnameAndAddressStreetInList(String lastname, Li
353354

354355
@Query(sort = "{ age : -1 }")
355356
List<Person> findByAgeGreaterThan(int age, Sort sort);
357+
358+
List<Person> findByFirstnameRegex(Pattern pattern);
356359
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
import java.lang.reflect.Method;
2626
import java.util.List;
27+
import java.util.regex.Pattern;
2728

29+
import org.bson.Document;
2830
import org.bson.types.ObjectId;
2931
import org.junit.Before;
3032
import org.junit.Rule;
@@ -60,8 +62,6 @@
6062
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
6163
import org.springframework.data.repository.query.parser.PartTree;
6264

63-
import org.bson.Document;
64-
6565
/**
6666
* Unit test for {@link MongoQueryCreator}.
6767
*
@@ -627,6 +627,25 @@ public void queryShouldThrowExceptionWhenArgumentDoesNotMatchDeclaration() {
627627
new MongoQueryCreator(tree, accessor, context).createQuery();
628628
}
629629

630+
@Test // DATAMONGO-2003
631+
public void createsRegexQueryForPatternCorrectly() throws Exception {
632+
633+
PartTree tree = new PartTree("findByFirstNameRegex", Person.class);
634+
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, Pattern.compile(".*")), context);
635+
636+
assertThat(creator.createQuery(), is(query(where("firstName").regex(".*"))));
637+
}
638+
639+
@Test // DATAMONGO-2003
640+
public void createsRegexQueryForPatternWithOptionsCorrectly() throws Exception {
641+
642+
Pattern pattern = Pattern.compile(".*", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
643+
644+
PartTree tree = new PartTree("findByFirstNameRegex", Person.class);
645+
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, pattern), context);
646+
assertThat(creator.createQuery(), is(query(where("firstName").regex(".*", "iu"))));
647+
}
648+
630649
interface PersonRepository extends Repository<Person, Long> {
631650

632651
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);

0 commit comments

Comments
 (0)