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
a4d6a0cf8a
commit
1ab130ffca
@@ -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;
|
||||
|
||||
@@ -1205,4 +1206,18 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
||||
public void findOptionalSingleEntityThrowsErrorWhenNotUnique() {
|
||||
repository.findOptionalPersonByLastnameLike(dave.getLastname());
|
||||
}
|
||||
|
||||
@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;
|
||||
@@ -41,7 +42,7 @@ import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Sample repository managing {@link Person} entities.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Christoph Strobl
|
||||
@@ -52,7 +53,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with the given lastname.
|
||||
*
|
||||
*
|
||||
* @param lastname
|
||||
* @return
|
||||
*/
|
||||
@@ -64,7 +65,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with the given lastname ordered by their firstname.
|
||||
*
|
||||
*
|
||||
* @param lastname
|
||||
* @return
|
||||
*/
|
||||
@@ -73,7 +74,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
/**
|
||||
* Returns the {@link Person}s with the given firstname. Uses {@link Query} annotation to define the query to be
|
||||
* executed.
|
||||
*
|
||||
*
|
||||
* @param firstname
|
||||
* @return
|
||||
*/
|
||||
@@ -86,7 +87,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with a firstname matching the given one (*-wildcard supported).
|
||||
*
|
||||
*
|
||||
* @param firstname
|
||||
* @return
|
||||
*/
|
||||
@@ -113,7 +114,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns a page of {@link Person}s with a lastname mathing the given one (*-wildcards supported).
|
||||
*
|
||||
*
|
||||
* @param lastname
|
||||
* @param pageable
|
||||
* @return
|
||||
@@ -125,7 +126,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with a firstname contained in the given varargs.
|
||||
*
|
||||
*
|
||||
* @param firstnames
|
||||
* @return
|
||||
*/
|
||||
@@ -133,7 +134,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with a firstname not contained in the given collection.
|
||||
*
|
||||
*
|
||||
* @param firstnames
|
||||
* @return
|
||||
*/
|
||||
@@ -143,7 +144,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with an age between the two given values.
|
||||
*
|
||||
*
|
||||
* @param from
|
||||
* @param to
|
||||
* @return
|
||||
@@ -152,7 +153,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns the {@link Person} with the given {@link Address} as shipping address.
|
||||
*
|
||||
*
|
||||
* @param address
|
||||
* @return
|
||||
*/
|
||||
@@ -160,7 +161,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
/**
|
||||
* Returns all {@link Person}s with the given {@link Address}.
|
||||
*
|
||||
*
|
||||
* @param address
|
||||
* @return
|
||||
*/
|
||||
@@ -347,4 +348,6 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
|
||||
|
||||
// DATAMONGO-1752
|
||||
Iterable<PersonSummary> findClosedProjectionBy();
|
||||
|
||||
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