DATAMONGO-1605 - Retain type of SpEL expression result when used in @Query.
Fix issue where any result of a SpEL expression had been treated as quoted String within the resulting MongoDB query.
This commit is contained in:
committed by
Mark Paluch
parent
61cb23db5c
commit
c77025859b
@@ -170,6 +170,12 @@ class ExpressionEvaluatingParameterBinder {
|
||||
|
||||
} else {
|
||||
|
||||
if (isExpression) {
|
||||
|
||||
buffer.deleteCharAt(quotationMarkIndex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (quotationMark == '\'') {
|
||||
buffer.replace(quotationMarkIndex, quotationMarkIndex + 1, "\"");
|
||||
}
|
||||
@@ -199,7 +205,7 @@ class ExpressionEvaluatingParameterBinder {
|
||||
return (String) value;
|
||||
}
|
||||
|
||||
return QuotedString.unquote(JSON.serialize(value));
|
||||
return binding.isExpression() ? JSON.serialize(value) : QuotedString.unquote(JSON.serialize(value));
|
||||
}
|
||||
|
||||
if (value instanceof byte[]) {
|
||||
@@ -213,6 +219,10 @@ class ExpressionEvaluatingParameterBinder {
|
||||
return base64representation;
|
||||
}
|
||||
|
||||
if (binding.isExpression() && value instanceof String) {
|
||||
return "\"" + JSON.serialize(value) + "\"";
|
||||
}
|
||||
|
||||
return JSON.serialize(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -509,6 +509,26 @@ public class StringBasedMongoQueryUnitTests {
|
||||
is((DBObject) JSON.parse("{ 'lastname' : { '$regex' : '^(calamity|John regalia|regalia)'} }")));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1605
|
||||
public void findUsingSpelShouldRetainParameterType() throws Exception {
|
||||
|
||||
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByUsingSpel", Object.class);
|
||||
ConvertingParameterAccessor accessor = StubParameterAccessor.getAccessor(converter, 100.01D);
|
||||
|
||||
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accessor);
|
||||
assertThat(query.getQueryObject(), is((DBObject) new BasicDBObject().append("arg0", 100.01D)));
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1605
|
||||
public void findUsingSpelShouldRetainNullValues() throws Exception {
|
||||
|
||||
StringBasedMongoQuery mongoQuery = createQueryForMethod("findByUsingSpel", Object.class);
|
||||
ConvertingParameterAccessor accessor = StubParameterAccessor.getAccessor(converter, new Object[]{null});
|
||||
|
||||
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(accessor);
|
||||
assertThat(query.getQueryObject(), is((DBObject) new BasicDBObject().append("arg0", null)));
|
||||
}
|
||||
|
||||
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
|
||||
|
||||
Method method = SampleRepository.class.getMethod(name, parameters);
|
||||
@@ -600,5 +620,8 @@ public class StringBasedMongoQueryUnitTests {
|
||||
|
||||
@Query("{ 'lastname' : { '$regex' : '^(?0|John ?1|?1)'} }") // use spel or some regex string this is fucking bad
|
||||
Person findByLastnameRegex(String lastname, String alternative);
|
||||
|
||||
@Query("{ arg0 : ?#{[0]} }")
|
||||
List<Person> findByUsingSpel(Object arg0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user