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

Original PR: #525
This commit is contained in:
Peter-Josef Meisch 2020-09-24 22:14:56 +02:00 committed by GitHub
parent 8d4c305732
commit 7117e5d70d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 10 deletions

View File

@ -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<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

@ -148,8 +148,14 @@ public class SimpleElasticsearchRepository<T, ID> implements ElasticsearchReposi
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 = execute(operations -> operations.multiGet(query, entityClass, getIndexCoordinates()));
if (multiGetEntities != null) {

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);
}
}