Skip to content

Commit 3dcf937

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 6373a70 commit 3dcf937

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
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
@@ -18,13 +18,14 @@
1818
import java.util.regex.Matcher;
1919
import java.util.regex.Pattern;
2020

21-
import org.bson.types.ObjectId;
2221
import org.slf4j.Logger;
2322
import org.slf4j.LoggerFactory;
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

+22-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
6464
List<Person> all;
6565

6666
@Before
67-
public void setUp() {
67+
public void setUp() throws InterruptedException {
6868

6969
repository.deleteAll();
7070

7171
dave = new Person("Dave", "Matthews", 42);
7272
oliver = new Person("Oliver August", "Matthews", 4);
7373
carter = new Person("Carter", "Beauford", 49);
74+
Thread.sleep(10);
7475
boyd = new Person("Boyd", "Tinsley", 45);
7576
stefan = new Person("Stefan", "Lessard", 34);
7677
leroi = new Person("Leroi", "Moore", 41);
@@ -430,4 +431,24 @@ public void executesQueryWithDBRefReferenceCorrectly() {
430431
assertThat(result.size(), is(1));
431432
assertThat(result, hasItem(dave));
432433
}
434+
435+
/**
436+
* @see DATAMONGO-425
437+
*/
438+
@Test
439+
public void bindsDateParameterForDerivedQueryCorrectly() {
440+
441+
List<Person> result = repository.findByCreatedAtLessThan(boyd.createdAt);
442+
assertThat(result.isEmpty(), is(false));
443+
}
444+
445+
/**
446+
* @see DATAMONGO-425
447+
*/
448+
@Test
449+
public void bindsDateParameterForManuallyDefinedQueryCorrectly() {
450+
451+
List<Person> result = repository.findByCreatedAtLessThanManually(boyd.createdAt);
452+
assertThat(result.isEmpty(), is(false));
453+
}
433454
}

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;
@@ -42,6 +43,7 @@ public enum Sex {
4243
private Integer age;
4344
@SuppressWarnings("unused")
4445
private Sex sex;
46+
Date createdAt;
4547

4648
@GeoSpatialIndexed
4749
private Point location;
@@ -75,6 +77,7 @@ public Person(String firstname, String lastname, Integer age, Sex sex) {
7577
this.age = age;
7678
this.sex = sex;
7779
this.email = (firstname == null ? "noone" : firstname.toLowerCase()) + "@dmband.com";
80+
this.createdAt = new Date();
7881
}
7982

8083
/**

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;
@@ -153,4 +154,15 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
153154
GeoPage<Person> findByLocationNear(Point point, Distance maxDistance, Pageable pageable);
154155

155156
List<Person> findByCreator(User user);
157+
158+
/**
159+
* @see DATAMONGO-425
160+
*/
161+
List<Person> findByCreatedAtLessThan(Date date);
162+
163+
/**
164+
* @see DATAMONGO-425
165+
*/
166+
@Query("{ 'createdAt' : { '$lt' : ?0 }}")
167+
List<Person> findByCreatedAtLessThanManually(Date date);
156168
}

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)