DATAMONGO-1072 - Fix annotated query placeholders not replaced correctly.

We now also check field names for potential placeholder matches to ensure those are registered for binding parameters.

Original pull request: #233.
This commit is contained in:
Christoph Strobl
2014-10-21 20:24:45 +02:00
committed by Thomas Darimont
parent 6cda9ab939
commit f8453825fb
4 changed files with 36 additions and 0 deletions

View File

@@ -262,6 +262,7 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
DBObject dbo = (DBObject) value;
for (String field : dbo.keySet()) {
collectParameterReferencesIntoBindings(bindings, field);
collectParameterReferencesIntoBindings(bindings, dbo.get(field));
}
}

View File

@@ -1048,4 +1048,16 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
slice = repository.findByAgeGreaterThan(50, slice.nextPageable());
assertThat(slice, contains(persons.subList(20, 40).toArray()));
}
/**
* @see DATAMONGO-1072
*/
@Test
public void shouldBindPlaceholdersUsedAsKeysCorrectly() {
List<Person> persons = repository.findByKeyValue("firstname", alicia.getFirstname());
assertThat(persons, hasSize(1));
assertThat(persons, hasItem(alicia));
}
}

View File

@@ -317,4 +317,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
* @see DATAMONGO-1030
*/
PersonSummary findSummaryByLastname(String lastname);
@Query("{ ?0 : ?1 }")
List<Person> findByKeyValue(String key, String value);
}

View File

@@ -43,6 +43,7 @@ import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.core.RepositoryMetadata;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
@@ -273,6 +274,21 @@ public class StringBasedMongoQueryUnitTests {
assertThat(dbRef.getRef(), is("reference"));
}
/**
* @see DATAMONGO-1072
*/
@Test
public void shouldParseJsonKeyReplacementCorrectly() throws Exception {
StringBasedMongoQuery mongoQuery = createQueryForMethod("methodWithPlaceholderInKeyOfJsonStructure", String.class,
String.class);
ConvertingParameterAccessor parameterAccessor = StubParameterAccessor.getAccessor(converter, "key", "value");
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(parameterAccessor);
assertThat(query.getQueryObject(), is(new BasicDBObjectBuilder().add("key", "value").get()));
}
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
Method method = SampleRepository.class.getMethod(name, parameters);
@@ -314,5 +330,9 @@ public class StringBasedMongoQueryUnitTests {
@Query("{ 'reference' : { $ref : 'reference', $id : ?0 }}")
Object methodWithManuallyDefinedDbRef(String id);
@Query("{ ?0 : ?1}")
Object methodWithPlaceholderInKeyOfJsonStructure(String keyReplacement, String valueReplacement);
}
}