DATAES-937 - Repository queries with IN filters fail with empty input list.

Original PR: #525

(cherry picked from commit 7117e5d70d7b52aa689787b2201a3f26639bbcf8)
This commit is contained in:
Peter-Josef Meisch 2020-09-24 22:37:14 +02:00
parent a88612df71
commit e293da89f8
No known key found for this signature in database
GPG Key ID: DE108246970C7708
3 changed files with 31 additions and 8 deletions

View File

@ -393,11 +393,6 @@ public class Criteria {
}
private List<Object> toCollection(Object... values) {
if (values.length == 0 || (values.length > 1 && values[1] instanceof Collection)) {
throw new InvalidDataAccessApiUsageException(
"At least one element " + (values.length > 0 ? ("of argument of type " + values[1].getClass().getName()) : "")
+ " has to be present.");
}
return Arrays.asList(values);
}

View File

@ -157,8 +157,14 @@ public abstract class AbstractElasticsearchRepository<T, ID> implements Elastics
Assert.notNull(ids, "ids can't be null.");
NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build();
List<T> result = new ArrayList<>();
List<String> stringIds = stringIdsRepresentation(ids);
if (stringIds.isEmpty()) {
return result;
}
NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIds).build();
List<T> multiGetEntities = operations.multiGet(query, getEntityClass(), getIndexCoordinates());
multiGetEntities.forEach(entity -> {

View File

@ -23,12 +23,14 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@ -249,7 +251,7 @@ class QueryKeywordsTests {
products.forEach(product -> assertThat(product.name).isEqualTo("Sugar"));
}
@Test
@Test // DATAES-239
void shouldSearchForNullValues() {
final List<Product> products = repository.findByName(null);
@ -257,7 +259,7 @@ class QueryKeywordsTests {
assertThat(products.get(0).getId()).isEqualTo("6");
}
@Test
@Test // DATAES-239
void shouldDeleteWithNullValues() {
repository.deleteByName(null);
@ -265,6 +267,24 @@ class QueryKeywordsTests {
assertThat(count).isEqualTo(5);
}
@Test // DATAES-937
@DisplayName("should return empty list on findById with empty input list")
void shouldReturnEmptyListOnFindByIdWithEmptyInputList() {
Iterable<Product> products = repository.findAllById(new ArrayList<>());
assertThat(products).isEmpty();
}
@Test // DATAES-937
@DisplayName("should return empty list on derived method with empty input list")
void shouldReturnEmptyListOnDerivedMethodWithEmptyInputList() {
Iterable<Product> products = repository.findAllByNameIn(new ArrayList<>());
assertThat(products).isEmpty();
}
/**
* @author Mohsin Husen
* @author Artur Konczak
@ -344,6 +364,8 @@ class QueryKeywordsTests {
List<Product> findTop2ByName(String name);
void deleteByName(@Nullable String name);
List<Product> findAllByNameIn(List<String> names);
}
}