diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/Criteria.java b/src/main/java/org/springframework/data/elasticsearch/core/query/Criteria.java index 0a2ed5a86..197a5e956 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/Criteria.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/Criteria.java @@ -393,11 +393,6 @@ public class Criteria { } private List 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); } diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java index b17053d28..fd8499298 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java @@ -157,8 +157,14 @@ public abstract class AbstractElasticsearchRepository implements Elastics Assert.notNull(ids, "ids can't be null."); - NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build(); List result = new ArrayList<>(); + List stringIds = stringIdsRepresentation(ids); + + if (stringIds.isEmpty()) { + return result; + } + + NativeSearchQuery query = new NativeSearchQueryBuilder().withIds(stringIds).build(); List multiGetEntities = operations.multiGet(query, getEntityClass(), getIndexCoordinates()); multiGetEntities.forEach(entity -> { diff --git a/src/test/java/org/springframework/data/elasticsearch/repository/query/keywords/QueryKeywordsTests.java b/src/test/java/org/springframework/data/elasticsearch/repository/query/keywords/QueryKeywordsTests.java index 6354ee8fd..e9cdaaa9b 100644 --- a/src/test/java/org/springframework/data/elasticsearch/repository/query/keywords/QueryKeywordsTests.java +++ b/src/test/java/org/springframework/data/elasticsearch/repository/query/keywords/QueryKeywordsTests.java @@ -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 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 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 products = repository.findAllByNameIn(new ArrayList<>()); + + assertThat(products).isEmpty(); + } + /** * @author Mohsin Husen * @author Artur Konczak @@ -344,6 +364,8 @@ class QueryKeywordsTests { List findTop2ByName(String name); void deleteByName(@Nullable String name); + + List findAllByNameIn(List names); } }