Skip to content

Commit 08e534a

Browse files
author
Thomas Darimont
committed
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.
1 parent 914acfe commit 08e534a

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ private void collectParameterReferencesIntoBindings(List<ParameterBinding> bindi
260260
DBObject dbo = (DBObject) value;
261261

262262
for (String field : dbo.keySet()) {
263+
collectParameterReferencesIntoBindings(bindings, field);
263264
collectParameterReferencesIntoBindings(bindings, dbo.get(field));
264265
}
265266
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,4 +963,16 @@ public void executesSingleEntityQueryWithProjectionCorrectly() {
963963
assertThat(result.lastname, is("Beauford"));
964964

965965
}
966+
967+
/**
968+
* @see DATAMONGO-1072
969+
*/
970+
@Test
971+
public void shouldBindPlaceholdersUsedAsKeysCorrectly() {
972+
973+
List<Person> persons = repository.findByKeyValue("firstname", alicia.getFirstname());
974+
975+
assertThat(persons, hasSize(1));
976+
assertThat(persons, hasItem(alicia));
977+
}
966978
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
307307
* @see DATAMONGO-1030
308308
*/
309309
PersonSummary findSummaryByLastname(String lastname);
310+
311+
@Query("{ ?0 : ?1 }")
312+
List<Person> findByKeyValue(String key, String value);
310313
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.data.repository.core.RepositoryMetadata;
4343

4444
import com.mongodb.BasicDBObject;
45+
import com.mongodb.BasicDBObjectBuilder;
4546
import com.mongodb.DBObject;
4647

4748
/**
@@ -255,6 +256,21 @@ public void bindsSimplePropertyWithRegexCorrectly() throws Exception {
255256
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
256257
}
257258

259+
/**
260+
* @see DATAMONGO-1072
261+
*/
262+
@Test
263+
public void shouldParseJsonKeyReplacementCorrectly() throws Exception {
264+
265+
StringBasedMongoQuery mongoQuery = createQueryForMethod("methodWithPlaceholderInKeyOfJsonStructure", String.class,
266+
String.class);
267+
ConvertingParameterAccessor parameterAccessor = StubParameterAccessor.getAccessor(converter, "key", "value");
268+
269+
org.springframework.data.mongodb.core.query.Query query = mongoQuery.createQuery(parameterAccessor);
270+
271+
assertThat(query.getQueryObject(), is(new BasicDBObjectBuilder().add("key", "value").get()));
272+
}
273+
258274
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
259275

260276
Method method = SampleRepository.class.getMethod(name, parameters);
@@ -293,5 +309,8 @@ private interface SampleRepository {
293309

294310
@Query(value = "{$where: 'return this.date.getUTCMonth() == ?2 && this.date.getUTCDay() == ?3;'}")
295311
List<DBObject> findByQueryWithParametersInExpression(int param1, int param2, int param3, int param4);
312+
313+
@Query("{ ?0 : ?1}")
314+
Object methodWithPlaceholderInKeyOfJsonStructure(String keyReplacement, String valueReplacement);
296315
}
297316
}

0 commit comments

Comments
 (0)