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 6b7d0fdb..130b13e9 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 @@ -16,7 +16,6 @@ package org.springframework.data.elasticsearch.core.query; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.LinkedHashSet; import java.util.LinkedList; @@ -438,7 +437,7 @@ public class Criteria { /** * Add a {@link OperationKey#IN} entry to the {@link #queryCriteriaEntries}. This will create a terms query, so don't * use it with text fields as these are analyzed and changed by Elasticsearch (converted to lowercase with the default - * analyzer). If used for Strings, these should be marked a sfield type Keyword. + * analyzer). If used for Strings, these should be marked as field type Keyword. * * @param values the argument to the operation * @return this object @@ -743,11 +742,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/SimpleElasticsearchRepository.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepository.java index c2110e33..53bf8cee 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepository.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/SimpleElasticsearchRepository.java @@ -148,8 +148,14 @@ public class SimpleElasticsearchRepository implements ElasticsearchReposi 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 = execute(operations -> operations.multiGet(query, entityClass, getIndexCoordinates())); if (multiGetEntities != null) { 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 6354ee8f..e9cdaaa9 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); } }