Skip to content

Commit 3be35cb

Browse files
committed
DATAMONGO-425 - Fixed parameter binding for Dates and manually defined queries.
Replaced manual JSON serialization for special parameters inside StringBasedMongoQuery by calling JSON.serialize(…).
1 parent 9421c45 commit 3be35cb

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

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

+6-19
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23-
import org.bson.types.ObjectId;
2423
import org.springframework.data.mongodb.core.MongoOperations;
2524
import org.springframework.data.mongodb.core.query.BasicQuery;
2625
import org.springframework.data.mongodb.core.query.Query;
2726

27+
import com.mongodb.util.JSON;
28+
2829
/**
2930
* Query to use a plain JSON String to create the {@link Query} to actually execute.
3031
*
@@ -55,12 +56,9 @@ public StringBasedMongoQuery(MongoQueryMethod method, MongoOperations mongoOpera
5556
}
5657

5758
/*
58-
* (non-Javadoc)
59-
*
60-
* @see
61-
* org.springframework.data.mongodb.repository.AbstractMongoQuery#createQuery(org.springframework.data.
62-
* repository.query.SimpleParameterAccessor, org.springframework.data.mongodb.core.core.support.convert.MongoConverter)
63-
*/
59+
* (non-Javadoc)
60+
* @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery#createQuery(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor)
61+
*/
6462
@Override
6563
protected Query createQuery(ConvertingParameterAccessor accessor) {
6664

@@ -99,17 +97,6 @@ private String replacePlaceholders(String input, ConvertingParameterAccessor acc
9997
}
10098

10199
private String getParameterWithIndex(ConvertingParameterAccessor accessor, int index) {
102-
103-
Object parameter = accessor.getBindableValue(index);
104-
105-
if (parameter == null) {
106-
return "null";
107-
} else if (parameter instanceof String || parameter.getClass().isEnum()) {
108-
return String.format("\"%s\"", parameter);
109-
} else if (parameter instanceof ObjectId) {
110-
return String.format("{ '$oid' : '%s' }", parameter);
111-
}
112-
113-
return parameter.toString();
100+
return JSON.serialize(accessor.getBindableValue(index));
114101
}
115102
}

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

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2012 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.
@@ -32,6 +32,7 @@
3232
import org.springframework.data.domain.PageRequest;
3333
import org.springframework.data.domain.Sort;
3434
import org.springframework.data.domain.Sort.Direction;
35+
import org.springframework.data.mongodb.core.MongoOperations;
3536
import org.springframework.data.mongodb.core.geo.Box;
3637
import org.springframework.data.mongodb.core.geo.Circle;
3738
import org.springframework.data.mongodb.core.geo.Distance;
@@ -53,19 +54,24 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
5354

5455
@Autowired
5556
protected PersonRepository repository;
57+
58+
@Autowired
59+
MongoOperations operations;
60+
5661
Person dave, oliver, carter, boyd, stefan, leroi, alicia;
5762
QPerson person;
5863

5964
List<Person> all;
6065

6166
@Before
62-
public void setUp() {
67+
public void setUp() throws InterruptedException {
6368

6469
repository.deleteAll();
6570

6671
dave = new Person("Dave", "Matthews", 42);
6772
oliver = new Person("Oliver August", "Matthews", 4);
6873
carter = new Person("Carter", "Beauford", 49);
74+
Thread.sleep(10);
6975
boyd = new Person("Boyd", "Tinsley", 45);
7076
stefan = new Person("Stefan", "Lessard", 34);
7177
leroi = new Person("Leroi", "Moore", 41);
@@ -396,4 +402,24 @@ public void considersSortForAnnotatedQuery() {
396402
assertThat(result.get(5), is(oliver));
397403
assertThat(result.get(6), is(stefan));
398404
}
405+
406+
/**
407+
* @see DATAMONGO-425
408+
*/
409+
@Test
410+
public void bindsDateParameterForDerivedQueryCorrectly() {
411+
412+
List<Person> result = repository.findByCreatedAtLessThan(boyd.createdAt);
413+
assertThat(result.isEmpty(), is(false));
414+
}
415+
416+
/**
417+
* @see DATAMONGO-425
418+
*/
419+
@Test
420+
public void bindsDateParameterForManuallyDefinedQueryCorrectly() {
421+
422+
List<Person> result = repository.findByCreatedAtLessThanManually(boyd.createdAt);
423+
assertThat(result.isEmpty(), is(false));
424+
}
399425
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18+
import java.util.Date;
1819
import java.util.Set;
1920

2021
import org.springframework.data.mongodb.core.geo.Point;
@@ -41,6 +42,7 @@ public enum Sex {
4142
private Integer age;
4243
@SuppressWarnings("unused")
4344
private Sex sex;
45+
Date createdAt;
4446

4547
@GeoSpatialIndexed
4648
private Point location;
@@ -71,6 +73,7 @@ public Person(String firstname, String lastname, Integer age, Sex sex) {
7173
this.age = age;
7274
this.sex = sex;
7375
this.email = (firstname == null ? "noone" : firstname.toLowerCase()) + "@dmband.com";
76+
this.createdAt = new Date();
7477
}
7578

7679
/**

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

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.mongodb.repository;
1717

1818
import java.util.Collection;
19+
import java.util.Date;
1920
import java.util.List;
2021

2122
import org.springframework.data.domain.Page;
@@ -151,4 +152,15 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
151152
GeoResults<Person> findByLocationNear(Point point, Distance maxDistance);
152153

153154
GeoPage<Person> findByLocationNear(Point point, Distance maxDistance, Pageable pageable);
155+
156+
/**
157+
* @see DATAMONGO-425
158+
*/
159+
List<Person> findByCreatedAtLessThan(Date date);
160+
161+
/**
162+
* @see DATAMONGO-425
163+
*/
164+
@Query("{ 'createdAt' : { '$lt' : ?0 }}")
165+
List<Person> findByCreatedAtLessThanManually(Date date);
154166
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
7777

7878
assertThat(indexInfo.isEmpty(), is(false));
7979
assertThat(indexInfo.size(), is(greaterThan(2)));
80-
assertThat(getIndexNamesFrom(indexInfo), hasItems("findByLastname", "findByFirstnameNotIn"));
80+
assertThat(getIndexNamesFrom(indexInfo), hasItems("findByLastnameLike", "findByFirstnameLike"));
8181

8282
return null;
8383
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/config/MongoNamespaceIntegrationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class MongoNamespaceIntegrationTests extends AbstractPersonRepositoryInte
2626

2727
@Before
2828
@Override
29-
public void setUp() {
29+
public void setUp() throws InterruptedException {
3030
super.setUp();
3131
factory = new DefaultListableBeanFactory();
3232
reader = new XmlBeanDefinitionReader(factory);

0 commit comments

Comments
 (0)