Skip to content

Commit 1f914a4

Browse files
committed
Handle Collection<> parameters to repository query methods.
Closes #1270.
1 parent e003e4c commit 1f914a4

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818
import static org.springframework.data.couchbase.core.query.N1QLExpression.x;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.Collections;
2123
import java.util.Formatter;
2224
import java.util.LinkedList;
2325
import java.util.List;
2426

2527
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
28+
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
29+
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
2630
import org.springframework.lang.Nullable;
2731

2832
import com.couchbase.client.core.error.InvalidArgumentException;
2933
import com.couchbase.client.java.json.JsonArray;
3034
import com.couchbase.client.java.json.JsonObject;
3135
import com.couchbase.client.java.json.JsonValue;
36+
import org.springframework.util.CollectionUtils;
3237

3338
/**
3439
* @author Michael Nitschinger
@@ -412,8 +417,8 @@ private String maybeWrapValue(N1QLExpression key, Object value, int[] paramIndex
412417
try {
413418
params.add(convert(converter, value));
414419
} catch (InvalidArgumentException iae) {
415-
if (value instanceof Object[]) {
416-
addAsArray(params, value, converter);
420+
if (value instanceof Object[] || value instanceof Collection) {
421+
addAsCollection(params, asCollection(value), converter);
417422
} else {
418423
throw iae;
419424
}
@@ -462,15 +467,28 @@ private static Object convert(CouchbaseConverter converter, Object value) {
462467
return converter != null ? converter.convertForWriteIfNeeded(value) : value;
463468
}
464469

465-
private void addAsArray(JsonArray posValues, Object o, CouchbaseConverter converter) {
466-
Object[] array = (Object[]) o;
470+
private void addAsCollection(JsonArray posValues, Collection collection, CouchbaseConverter converter) {
467471
JsonArray ja = JsonValue.ja();
468-
for (Object e : array) {
472+
for (Object e : collection) {
469473
ja.add(String.valueOf(convert(converter, e)));
470474
}
471475
posValues.add(ja);
472476
}
473477

478+
/**
479+
* Returns a collection from the given source object. From MappingCouchbaseConverter.
480+
*
481+
* @param source the source object.
482+
* @return the target collection.
483+
*/
484+
private static Collection<?> asCollection(final Object source) {
485+
if (source instanceof Collection) {
486+
return (Collection<?>) source;
487+
}
488+
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
489+
}
490+
491+
474492
private String maybeBackTic(String value) {
475493
if (value == null || (value.startsWith("`") && value.endsWith("`"))) {
476494
return value;

src/test/java/org/springframework/data/couchbase/domain/AirportRepository.java

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public interface AirportRepository extends CouchbaseRepository<Airport, String>,
7777
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
7878
Airport findByIata(Iata iata);
7979

80+
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
81+
Airport findByIataIn(java.util.Collection<Iata> iatas);
82+
83+
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
84+
Airport findByIataIn(Iata[] iata);
85+
8086
// NOT_BOUNDED to test ScanConsistency
8187
// @ScanConsistency(query = QueryScanConsistency.NOT_BOUNDED)
8288
Airport iata(String iata);

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import org.springframework.data.couchbase.domain.AirportMini;
7070
import org.springframework.data.couchbase.domain.AirportRepository;
7171
import org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest;
72+
import org.springframework.data.couchbase.domain.Iata;
7273
import org.springframework.data.couchbase.domain.NaiveAuditorAware;
7374
import org.springframework.data.couchbase.domain.Person;
7475
import org.springframework.data.couchbase.domain.PersonRepository;
@@ -370,10 +371,21 @@ void findByEnum() {
370371
try {
371372
vie = new Airport("airports::vie", "vie", "loww");
372373
vie = airportRepository.save(vie);
373-
Airport airport2 = airportRepository.findByIata(vie.getIata());
374+
Airport airport2 = airportRepository.findByIata(Iata.vie);
374375
assertNotNull(airport2, "should have found " + vie);
375376
assertEquals(airport2.getId(), vie.getId());
376377

378+
Airport airport3 = airportRepository.findByIataIn(new Iata[]{Iata.vie, Iata.xxx});
379+
assertNotNull(airport3, "should have found " + vie);
380+
assertEquals(airport3.getId(), vie.getId());
381+
382+
java.util.Collection<Iata> iatas = new ArrayList<>();
383+
iatas.add(Iata.vie);
384+
iatas.add(Iata.xxx);
385+
Airport airport4 = airportRepository.findByIataIn( iatas );
386+
assertNotNull(airport4, "should have found " + vie);
387+
assertEquals(airport4.getId(), vie.getId());
388+
377389
} finally {
378390
airportRepository.delete(vie);
379391
}

0 commit comments

Comments
 (0)