diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java index 349df7a2c..4c1da7bd9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQuery.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,8 @@ */ package org.springframework.data.mongodb.repository.query; +import com.mongodb.DBObject; +import com.mongodb.util.JSON; import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.MongoTemplate; @@ -110,7 +112,7 @@ public class PartTreeMongoQuery extends AbstractMongoQuery { try { - BasicQuery result = new BasicQuery(query.getQueryObject().toString(), fieldSpec); + BasicQuery result = new BasicQuery(query.getQueryObject(), (DBObject) JSON.parse(fieldSpec)); result.setSortObject(query.getSortObject()); return result; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java index dc61cfbfd..9e578c9df 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import static org.mockito.Mockito.*; import static org.springframework.data.mongodb.core.query.IsTextQuery.*; import java.lang.reflect.Method; +import java.util.List; import org.junit.Before; import org.junit.Rule; @@ -40,6 +41,7 @@ import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.TextCriteria; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.Person; +import org.springframework.data.mongodb.repository.Person.Sex; import org.springframework.data.mongodb.repository.Query; import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; @@ -192,6 +194,18 @@ public class PartTreeMongoQueryUnitTests { assertThat(fields.get("age"), is((Object) 1)); } + /** + * @see DATAMONGO-1500 + */ + @Test + public void shouldLeaveParameterConversionToQueryMapper() { + + org.springframework.data.mongodb.core.query.Query query = deriveQueryFromMethod("findBySex", Sex.FEMALE); + + assertThat(query.getQueryObject().get("sex"), is((Object) Sex.FEMALE)); + assertThat(query.getFieldsObject().get("firstname"), is((Object) 1)); + } + private org.springframework.data.mongodb.core.query.Query deriveQueryFromMethod(String method, Object... args) { Class[] types = new Class[args.length]; @@ -249,6 +263,9 @@ public class PartTreeMongoQueryUnitTests { PersonDto findPersonDtoByAge(Integer age); T findDynamicallyProjectedBy(Class type); + + @Query(fields = "{ 'firstname' : 1 }") + List findBySex(Sex sex); } interface PersonProjection {