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.
This commit is contained in:
committed by
Mark Paluch
parent
18313db8fb
commit
0c0f47f76f
@@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -206,7 +207,9 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
|
||||
case NOT_CONTAINING:
|
||||
return createContainingCriteria(part, property, criteria.not(), parameters);
|
||||
case REGEX:
|
||||
return criteria.regex(parameters.next().toString());
|
||||
|
||||
Object param = parameters.next();
|
||||
return param instanceof Pattern ? criteria.regex((Pattern) param) : criteria.regex(param.toString());
|
||||
case EXISTS:
|
||||
return criteria.exists((Boolean) parameters.next());
|
||||
case TRUE:
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -1216,4 +1217,18 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
assertThat(repository.findByAgeGreaterThan(40, Sort.by(Direction.ASC, "age"))).containsExactly(leroi, dave, boyd,
|
||||
carter);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2003
|
||||
public void findByRegexWithPattern() {
|
||||
assertThat(repository.findByFirstnameRegex(Pattern.compile(alicia.getFirstname()))).hasSize(1);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2003
|
||||
public void findByRegexWithPatternAndOptions() {
|
||||
|
||||
String fn = alicia.getFirstname().toUpperCase();
|
||||
|
||||
assertThat(repository.findByFirstnameRegex(Pattern.compile(fn))).hasSize(0);
|
||||
assertThat(repository.findByFirstnameRegex(Pattern.compile(fn, Pattern.CASE_INSENSITIVE))).hasSize(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -353,4 +354,6 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
@Query(sort = "{ age : -1 }")
|
||||
List<Person> findByAgeGreaterThan(int age, Sort sort);
|
||||
|
||||
List<Person> findByFirstnameRegex(Pattern pattern);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,9 @@ import static org.springframework.data.mongodb.repository.query.StubParameterAcc
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
@@ -60,8 +62,6 @@ import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
* Unit test for {@link MongoQueryCreator}.
|
||||
*
|
||||
@@ -627,6 +627,25 @@ public class MongoQueryCreatorUnitTests {
|
||||
new MongoQueryCreator(tree, accessor, context).createQuery();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2003
|
||||
public void createsRegexQueryForPatternCorrectly() throws Exception {
|
||||
|
||||
PartTree tree = new PartTree("findByFirstNameRegex", Person.class);
|
||||
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, Pattern.compile(".*")), context);
|
||||
|
||||
assertThat(creator.createQuery(), is(query(where("firstName").regex(".*"))));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2003
|
||||
public void createsRegexQueryForPatternWithOptionsCorrectly() throws Exception {
|
||||
|
||||
Pattern pattern = Pattern.compile(".*", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
|
||||
|
||||
PartTree tree = new PartTree("findByFirstNameRegex", Person.class);
|
||||
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, pattern), context);
|
||||
assertThat(creator.createQuery(), is(query(where("firstName").regex(".*", "iu"))));
|
||||
}
|
||||
|
||||
interface PersonRepository extends Repository<Person, Long> {
|
||||
|
||||
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);
|
||||
|
||||
Reference in New Issue
Block a user