Skip to content

Commit 2ae75a4

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1500 - Fix JSON serialization error in derived queries with field spec.
We now make sure not to eagerly attempt to convert given query parameters into a mongo specific format by calling toString() the query object, but rather delegate this to another step later in the chain. Original pull request: #404.
1 parent 9c20da3 commit 2ae75a4

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-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.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.query;
1717

18+
import com.mongodb.DBObject;
19+
import com.mongodb.util.JSON;
1820
import org.springframework.data.mapping.context.MappingContext;
1921
import org.springframework.data.mongodb.core.MongoOperations;
2022
import org.springframework.data.mongodb.core.MongoTemplate;
@@ -110,7 +112,7 @@ protected Query createQuery(ConvertingParameterAccessor accessor) {
110112

111113
try {
112114

113-
BasicQuery result = new BasicQuery(query.getQueryObject().toString(), fieldSpec);
115+
BasicQuery result = new BasicQuery(query.getQueryObject(), (DBObject) JSON.parse(fieldSpec));
114116
result.setSortObject(query.getSortObject());
115117

116118
return result;

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-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.
@@ -21,6 +21,7 @@
2121
import static org.springframework.data.mongodb.core.query.IsTextQuery.*;
2222

2323
import java.lang.reflect.Method;
24+
import java.util.List;
2425

2526
import org.junit.Before;
2627
import org.junit.Rule;
@@ -40,6 +41,7 @@
4041
import org.springframework.data.mongodb.core.query.TextCriteria;
4142
import org.springframework.data.mongodb.repository.MongoRepository;
4243
import org.springframework.data.mongodb.repository.Person;
44+
import org.springframework.data.mongodb.repository.Person.Sex;
4345
import org.springframework.data.mongodb.repository.Query;
4446
import org.springframework.data.projection.ProjectionFactory;
4547
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
@@ -192,6 +194,18 @@ public void usesDynamicProjection() {
192194
assertThat(fields.get("age"), is((Object) 1));
193195
}
194196

197+
/**
198+
* @see DATAMONGO-1500
199+
*/
200+
@Test
201+
public void shouldLeaveParameterConversionToQueryMapper() {
202+
203+
org.springframework.data.mongodb.core.query.Query query = deriveQueryFromMethod("findBySex", Sex.FEMALE);
204+
205+
assertThat(query.getQueryObject().get("sex"), is((Object) Sex.FEMALE));
206+
assertThat(query.getFieldsObject().get("firstname"), is((Object) 1));
207+
}
208+
195209
private org.springframework.data.mongodb.core.query.Query deriveQueryFromMethod(String method, Object... args) {
196210

197211
Class<?>[] types = new Class<?>[args.length];
@@ -249,6 +263,9 @@ interface Repo extends MongoRepository<Person, Long> {
249263
PersonDto findPersonDtoByAge(Integer age);
250264

251265
<T> T findDynamicallyProjectedBy(Class<T> type);
266+
267+
@Query(fields = "{ 'firstname' : 1 }")
268+
List<Person> findBySex(Sex sex);
252269
}
253270

254271
interface PersonProjection {

0 commit comments

Comments
 (0)