Skip to content

Commit 6b6d2aa

Browse files
committed
DATAMONGO-1445 - Prevent eager conversion of query parameters.
We now removed all eager query parameter conversions during query creation to be sure that the query mapping later on sees all to property names and is able to derive proper conversions.
1 parent b038984 commit 6b6d2aa

File tree

2 files changed

+43
-50
lines changed

2 files changed

+43
-50
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,16 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
177177
switch (type) {
178178
case AFTER:
179179
case GREATER_THAN:
180-
return criteria.gt(parameters.nextConverted(property));
180+
return criteria.gt(parameters.next());
181181
case GREATER_THAN_EQUAL:
182-
return criteria.gte(parameters.nextConverted(property));
182+
return criteria.gte(parameters.next());
183183
case BEFORE:
184184
case LESS_THAN:
185-
return criteria.lt(parameters.nextConverted(property));
185+
return criteria.lt(parameters.next());
186186
case LESS_THAN_EQUAL:
187-
return criteria.lte(parameters.nextConverted(property));
187+
return criteria.lte(parameters.next());
188188
case BETWEEN:
189-
return criteria.gt(parameters.nextConverted(property)).lt(parameters.nextConverted(property));
189+
return criteria.gt(parameters.next()).lt(parameters.next());
190190
case IS_NOT_NULL:
191191
return criteria.ne(null);
192192
case IS_NULL:
@@ -241,12 +241,12 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
241241
return criteria.within((Shape) parameter);
242242
case SIMPLE_PROPERTY:
243243

244-
return isSimpleComparisionPossible(part) ? criteria.is(parameters.nextConverted(property))
244+
return isSimpleComparisionPossible(part) ? criteria.is(parameters.next())
245245
: createLikeRegexCriteriaOrThrow(part, property, criteria, parameters, false);
246246

247247
case NEGATING_SIMPLE_PROPERTY:
248248

249-
return isSimpleComparisionPossible(part) ? criteria.ne(parameters.nextConverted(property))
249+
return isSimpleComparisionPossible(part) ? criteria.ne(parameters.next())
250250
: createLikeRegexCriteriaOrThrow(part, property, criteria, parameters, true);
251251
default:
252252
throw new IllegalArgumentException("Unsupported keyword!");

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

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,47 +17,42 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20-
import static org.mockito.Matchers.*;
21-
import static org.mockito.Mockito.*;
2220
import static org.springframework.data.mongodb.core.query.Criteria.*;
2321
import static org.springframework.data.mongodb.core.query.Query.*;
2422
import static org.springframework.data.mongodb.repository.query.StubParameterAccessor.*;
2523

2624
import java.lang.reflect.Method;
25+
import java.math.BigInteger;
2726
import java.util.List;
2827

28+
import org.bson.types.ObjectId;
2929
import org.junit.Before;
3030
import org.junit.Rule;
3131
import org.junit.Test;
3232
import org.junit.rules.ExpectedException;
3333
import org.junit.runner.RunWith;
3434
import org.mockito.Mock;
35-
import org.mockito.Mockito;
36-
import org.mockito.invocation.InvocationOnMock;
3735
import org.mockito.runners.MockitoJUnitRunner;
38-
import org.mockito.stubbing.Answer;
3936
import org.springframework.data.domain.Range;
4037
import org.springframework.data.geo.Distance;
4138
import org.springframework.data.geo.Metrics;
4239
import org.springframework.data.geo.Point;
4340
import org.springframework.data.geo.Polygon;
4441
import org.springframework.data.geo.Shape;
45-
import org.springframework.data.mapping.context.MappingContext;
4642
import org.springframework.data.mongodb.core.Person;
4743
import org.springframework.data.mongodb.core.Venue;
48-
import org.springframework.data.mongodb.core.convert.MongoConverter;
44+
import org.springframework.data.mongodb.core.convert.DbRefResolver;
45+
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
4946
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
5047
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
5148
import org.springframework.data.mongodb.core.mapping.DBRef;
5249
import org.springframework.data.mongodb.core.mapping.Field;
5350
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
54-
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
5551
import org.springframework.data.mongodb.core.query.Criteria;
5652
import org.springframework.data.mongodb.core.query.Query;
5753
import org.springframework.data.repository.Repository;
5854
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
5955
import org.springframework.data.repository.query.parser.PartTree;
60-
import org.springframework.data.util.TypeInformation;
6156

6257
/**
6358
* Unit test for {@link MongoQueryCreator}.
@@ -71,22 +66,20 @@ public class MongoQueryCreatorUnitTests {
7166

7267
Method findByFirstname, findByFirstnameAndFriend, findByFirstnameNotNull;
7368

74-
@Mock MongoConverter converter;
69+
@Mock DbRefResolver resolver;
7570

76-
MappingContext<?, MongoPersistentProperty> context;
71+
MongoMappingContext context;
72+
MappingMongoConverter converter;
7773

7874
@Rule public ExpectedException expection = ExpectedException.none();
7975

8076
@Before
8177
public void setUp() throws SecurityException, NoSuchMethodException {
8278

83-
context = new MongoMappingContext();
79+
this.context = new MongoMappingContext();
8480

85-
doAnswer(new Answer<Object>() {
86-
public Object answer(InvocationOnMock invocation) throws Throwable {
87-
return invocation.getArguments()[0];
88-
}
89-
}).when(converter).convertToMongoType(any(), Mockito.any(TypeInformation.class));
81+
this.converter = new MappingMongoConverter(resolver, context);
82+
this.converter.afterPropertiesSet();
9083
}
9184

9285
@Test
@@ -137,8 +130,8 @@ public void bindsMetricDistanceParameterToNearSphereCorrectly() throws Exception
137130
Point point = new Point(10, 20);
138131
Distance distance = new Distance(2.5, Metrics.KILOMETERS);
139132

140-
Query query = query(where("location").nearSphere(point).maxDistance(distance.getNormalizedValue()).and("firstname")
141-
.is("Dave"));
133+
Query query = query(
134+
where("location").nearSphere(point).maxDistance(distance.getNormalizedValue()).and("firstname").is("Dave"));
142135
assertBindsDistanceToQuery(point, distance, query);
143136
}
144137

@@ -148,8 +141,8 @@ public void bindsDistanceParameterToNearCorrectly() throws Exception {
148141
Point point = new Point(10, 20);
149142
Distance distance = new Distance(2.5);
150143

151-
Query query = query(where("location").near(point).maxDistance(distance.getNormalizedValue()).and("firstname")
152-
.is("Dave"));
144+
Query query = query(
145+
where("location").near(point).maxDistance(distance.getNormalizedValue()).and("firstname").is("Dave"));
153146
assertBindsDistanceToQuery(point, distance, query);
154147
}
155148

@@ -234,23 +227,6 @@ public void createsOrQueryCorrectly() {
234227
assertThat(query, is(query(new Criteria().orOperator(where("firstName").is("Dave"), where("age").is(42)))));
235228
}
236229

237-
/**
238-
* @see DATAMONGO-347
239-
*/
240-
@Test
241-
public void createsQueryReferencingADBRefCorrectly() {
242-
243-
User user = new User();
244-
com.mongodb.DBRef dbref = new com.mongodb.DBRef("user", "id");
245-
when(converter.toDBRef(eq(user), Mockito.any(MongoPersistentProperty.class))).thenReturn(dbref);
246-
247-
PartTree tree = new PartTree("findByCreator", User.class);
248-
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, user), context);
249-
Query query = creator.createQuery();
250-
251-
assertThat(query, is(query(where("creator").is(dbref))));
252-
}
253-
254230
/**
255231
* @see DATAMONGO-418
256232
*/
@@ -292,16 +268,14 @@ public void createsQueryWithContainingPredicateCorrectly() {
292268

293269
private void assertBindsDistanceToQuery(Point point, Distance distance, Query reference) throws Exception {
294270

295-
when(converter.convertToMongoType("Dave")).thenReturn("Dave");
296-
297271
PartTree tree = new PartTree("findByLocationNearAndFirstname",
298272
org.springframework.data.mongodb.repository.Person.class);
299273
Method method = PersonRepository.class.getMethod("findByLocationNearAndFirstname", Point.class, Distance.class,
300274
String.class);
301275
MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(PersonRepository.class),
302276
new MongoMappingContext());
303-
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(queryMethod, new Object[] { point, distance,
304-
"Dave" });
277+
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(queryMethod,
278+
new Object[] { point, distance, "Dave" });
305279

306280
Query query = new MongoQueryCreator(tree, new ConvertingParameterAccessor(converter, accessor), context)
307281
.createQuery();
@@ -676,6 +650,21 @@ public void bindsNullValueToContainsClause() {
676650
assertThat(query, is(query(where("emailAddresses").in((Object) null))));
677651
}
678652

653+
/**
654+
* @see DATAMONGO-1445
655+
*/
656+
@Test
657+
public void doesNotPreConvertValues() {
658+
659+
PartTree tree = new PartTree("id", WithBigIntegerId.class);
660+
BigInteger id = new BigInteger(new ObjectId().toString(), 16);
661+
ConvertingParameterAccessor accessor = getAccessor(converter, new Object[] { id });
662+
663+
Query query = new MongoQueryCreator(tree, accessor, context).createQuery();
664+
665+
assertThat(query, is(query(where("id").is(id))));
666+
}
667+
679668
interface PersonRepository extends Repository<Person, Long> {
680669

681670
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);
@@ -704,4 +693,8 @@ static class Address2dSphere {
704693
String street;
705694
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) Point geo;
706695
}
696+
697+
static class WithBigIntegerId {
698+
BigInteger id;
699+
}
707700
}

0 commit comments

Comments
 (0)