From 71fbb910ec30443aae4ef5d08c16a6b76ef2b2dd Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 10 Feb 2017 09:36:32 +0100 Subject: [PATCH] DATAMONGO-1608 - Polishing. Throw an IllegalArgumentException when trying to create a query using 'null' as an argument for queries resulting in a $regex query operator. Original Pull Request: #439 --- .../repository/query/MongoQueryCreator.java | 16 ++++++++++------ ...stractPersonRepositoryIntegrationTests.java | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 8 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 276de5d2c..769b2da37 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 @@ -300,9 +300,7 @@ class MongoQueryCreator extends AbstractQueryCreator { criteria = criteria.not(); } - Object next = parameters.next(); - - return addAppropriateLikeRegexTo(criteria, part, next != null ? next.toString() : ""); + return addAppropriateLikeRegexTo(criteria, part, parameters.next()); case NEVER: // intentional no-op @@ -330,7 +328,7 @@ class MongoQueryCreator extends AbstractQueryCreator { return criteria.in(nextAsArray(parameters)); } - return addAppropriateLikeRegexTo(criteria, part, parameters.next().toString()); + return addAppropriateLikeRegexTo(criteria, part, parameters.next()); } /** @@ -341,9 +339,15 @@ class MongoQueryCreator extends AbstractQueryCreator { * @param value * @return the criteria extended with the regex. */ - private Criteria addAppropriateLikeRegexTo(Criteria criteria, Part part, String value) { + private Criteria addAppropriateLikeRegexTo(Criteria criteria, Part part, Object value) { - return criteria.regex(toLikeRegex(value, part), toRegexOptions(part)); + if (value == null) { + + throw new IllegalArgumentException(String.format( + "Argument for creating $regex pattern for property '%s' must not be null!", part.getProperty().getSegment())); + } + + return criteria.regex(toLikeRegex(value.toString(), part), toRegexOptions(part)); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java index 3bb60ff39..319f2aa47 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java @@ -30,7 +30,9 @@ import java.util.stream.Stream; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DuplicateKeyException; @@ -72,6 +74,8 @@ import org.springframework.test.util.ReflectionTestUtils; @RunWith(SpringJUnit4ClassRunner.class) public abstract class AbstractPersonRepositoryIntegrationTests { + public @Rule ExpectedException expectedException = ExpectedException.none(); + @Autowired protected PersonRepository repository; @Autowired MongoOperations operations; @@ -171,6 +175,15 @@ public abstract class AbstractPersonRepositoryIntegrationTests { assertThat(result, hasItem(boyd)); } + @Test // DATAMONGO-1608 + public void findByFirstnameLikeWithNull() { + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("property 'firstname'"); + + repository.findByFirstnameLike(null); + } + @Test public void findsPagedPersons() throws Exception { @@ -658,9 +671,10 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @Test // DATAMONGO-1608 public void findByFirstNameIgnoreCaseWithNull() { - List result = repository.findByFirstnameIgnoreCase(null); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("property 'firstname'"); - assertThat(result.size(), is(0)); + repository.findByFirstnameIgnoreCase(null); } @Test // DATAMONGO-770