Skip to content

Commit 1ab130f

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 a4d6a0c commit 1ab130f

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
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

@@ -1205,4 +1206,18 @@ public void findSingleEntityThrowsErrorWhenNotUnique() {
12051206
public void findOptionalSingleEntityThrowsErrorWhenNotUnique() {
12061207
repository.findOptionalPersonByLastnameLike(dave.getLastname());
12071208
}
1209+
1210+
@Test // DATAMONGO-2003
1211+
public void findByRegexWithPattern() {
1212+
assertThat(repository.findByFirstnameRegex(Pattern.compile(alicia.getFirstname()))).hasSize(1);
1213+
}
1214+
1215+
@Test // DATAMONGO-2003
1216+
public void findByRegexWithPatternAndOptions() {
1217+
1218+
String fn = alicia.getFirstname().toUpperCase();
1219+
1220+
assertThat(repository.findByFirstnameRegex(Pattern.compile(fn))).hasSize(0);
1221+
assertThat(repository.findByFirstnameRegex(Pattern.compile(fn, Pattern.CASE_INSENSITIVE))).hasSize(1);
1222+
}
12081223
}

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

Lines changed: 14 additions & 11 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;
@@ -41,7 +42,7 @@
4142

4243
/**
4344
* Sample repository managing {@link Person} entities.
44-
*
45+
*
4546
* @author Oliver Gierke
4647
* @author Thomas Darimont
4748
* @author Christoph Strobl
@@ -52,7 +53,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
5253

5354
/**
5455
* Returns all {@link Person}s with the given lastname.
55-
*
56+
*
5657
* @param lastname
5758
* @return
5859
*/
@@ -64,7 +65,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
6465

6566
/**
6667
* Returns all {@link Person}s with the given lastname ordered by their firstname.
67-
*
68+
*
6869
* @param lastname
6970
* @return
7071
*/
@@ -73,7 +74,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
7374
/**
7475
* Returns the {@link Person}s with the given firstname. Uses {@link Query} annotation to define the query to be
7576
* executed.
76-
*
77+
*
7778
* @param firstname
7879
* @return
7980
*/
@@ -86,7 +87,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
8687

8788
/**
8889
* Returns all {@link Person}s with a firstname matching the given one (*-wildcard supported).
89-
*
90+
*
9091
* @param firstname
9192
* @return
9293
*/
@@ -113,7 +114,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
113114

114115
/**
115116
* Returns a page of {@link Person}s with a lastname mathing the given one (*-wildcards supported).
116-
*
117+
*
117118
* @param lastname
118119
* @param pageable
119120
* @return
@@ -125,15 +126,15 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
125126

126127
/**
127128
* Returns all {@link Person}s with a firstname contained in the given varargs.
128-
*
129+
*
129130
* @param firstnames
130131
* @return
131132
*/
132133
List<Person> findByFirstnameIn(String... firstnames);
133134

134135
/**
135136
* Returns all {@link Person}s with a firstname not contained in the given collection.
136-
*
137+
*
137138
* @param firstnames
138139
* @return
139140
*/
@@ -143,7 +144,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
143144

144145
/**
145146
* Returns all {@link Person}s with an age between the two given values.
146-
*
147+
*
147148
* @param from
148149
* @param to
149150
* @return
@@ -152,15 +153,15 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
152153

153154
/**
154155
* Returns the {@link Person} with the given {@link Address} as shipping address.
155-
*
156+
*
156157
* @param address
157158
* @return
158159
*/
159160
Person findByShippingAddresses(Address address);
160161

161162
/**
162163
* Returns all {@link Person}s with the given {@link Address}.
163-
*
164+
*
164165
* @param address
165166
* @return
166167
*/
@@ -347,4 +348,6 @@ Page<Person> findByCustomQueryLastnameAndAddressStreetInList(String lastname, Li
347348

348349
// DATAMONGO-1752
349350
Iterable<PersonSummary> findClosedProjectionBy();
351+
352+
List<Person> findByFirstnameRegex(Pattern pattern);
350353
}

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)