Skip to content

Commit 07b8e82

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2119 - Allow SpEL usage for annotated $regex query.
Original pull request: spring-projects#621.
1 parent 8f4db55 commit 07b8e82

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import java.util.List;
2121
import java.util.regex.Matcher;
2222
import java.util.regex.Pattern;
23+
import java.util.regex.PatternSyntaxException;
2324

25+
import org.bson.BSON;
2426
import org.bson.Document;
2527
import org.slf4j.Logger;
2628
import org.slf4j.LoggerFactory;
@@ -37,6 +39,7 @@
3739
import com.mongodb.DBObject;
3840
import com.mongodb.DBRef;
3941
import com.mongodb.util.JSON;
42+
import com.mongodb.util.JSONCallback;
4043

4144
/**
4245
* Query to use a plain JSON String to create the {@link Query} to actually execute.
@@ -225,7 +228,36 @@ public String parseAndCollectParameterBindingsFromQueryIntoBindings(String input
225228
String transformedInput = transformQueryAndCollectExpressionParametersIntoBindings(input, bindings);
226229
String parseableInput = makeParameterReferencesParseable(transformedInput);
227230

228-
collectParameterReferencesIntoBindings(bindings, JSON.parse(parseableInput));
231+
collectParameterReferencesIntoBindings(bindings, JSON.parse(parseableInput, new JSONCallback() {
232+
233+
/*
234+
* (non-Javadoc)
235+
* @see com.mongodb.util.JSONCallback#objectDone()
236+
*/
237+
@Override
238+
public Object objectDone() {
239+
return exceptionSwallowingStackReducingObjectDone();
240+
}
241+
242+
private Object exceptionSwallowingStackReducingObjectDone/*CauseWeJustNeedTheStructureNotTheActualValue*/() {
243+
244+
Object value;
245+
246+
try {
247+
return super.objectDone();
248+
} catch (PatternSyntaxException e) {
249+
value = Pattern.compile("");
250+
}
251+
252+
if (!isStackEmpty()) {
253+
_put(curName(), value);
254+
} else {
255+
value = !BSON.hasDecodeHooks() ? value : BSON.applyDecodingHooks(value);
256+
setRoot(value);
257+
}
258+
return value;
259+
}
260+
}));
229261

230262
return transformedInput;
231263
}

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,19 @@ public void findUsingSpelShouldRetainNullValues() {
590590
assertThat(query.getQueryObject(), is(new Document("arg0", null)));
591591
}
592592

593+
@Test // DATAMONGO-2119
594+
public void spelShouldIgnoreJsonParseErrorsForRegex() {
595+
596+
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByPersonLastnameRegex", Person.class);
597+
ConvertingParameterAccessor accessor = StubParameterAccessor.getAccessor(converter,
598+
new Person("Molly", "Chandler"));
599+
600+
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accessor);
601+
602+
assertThat(query.getQueryObject().toJson(),
603+
is(new BasicQuery("{lastname: {$regex: 'Chandler'}}").getQueryObject().toJson()));
604+
}
605+
593606
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) {
594607

595608
try {
@@ -697,10 +710,14 @@ private interface SampleRepository extends Repository<Person, Long> {
697710
@Query("{ 'arg0' : '?0', 'arg1' : '?1s' }")
698711
List<Person> findByWhenQuotedAndSomeStuffAppended(String arg0, String arg1);
699712

700-
@Query("{ 'lastname' : { '$regex' : '^(?0|John ?1|?1)'} }") // use spel or some regex string this is fucking bad
713+
@Query("{ 'lastname' : { '$regex' : '^(?0|John ?1|?1)'} }") // use spel or some regex string this is bad
701714
Person findByLastnameRegex(String lastname, String alternative);
702715

703716
@Query("{ arg0 : ?#{[0]} }")
704717
List<Person> findByUsingSpel(Object arg0);
718+
719+
@Query("{ 'lastname' : { '$regex' : ?#{[0].lastname} } }")
720+
Person findByPersonLastnameRegex(Person key);
705721
}
722+
706723
}

0 commit comments

Comments
 (0)