Skip to content

Commit

Permalink
fix: prevent index out of bounds on nocontent empty query results (#353)
Browse files Browse the repository at this point in the history
fix: prevent index out of bounds on nocontent empty query results (resolves gh-351)
  • Loading branch information
bsbodden authored Sep 23, 2023
1 parent fce58bf commit 4d087fe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,13 @@ private Stream<T> resolveStream() {
if (useNoContent) {
query.setNoContent();
SearchResult searchResult = entitySearchStream.getOps().search(query);
String keySample = searchResult.getDocuments().get(0).getId();
int idBegin = keySample.indexOf(":") + 1;
resolvedStream = (Stream<T>) searchResult.getDocuments().stream().map(Document::getId).map(key -> key.substring(idBegin));
if (searchResult.getDocuments().size() > 0) {
String keySample = searchResult.getDocuments().get(0).getId();
int idBegin = keySample.indexOf(":") + 1;
resolvedStream = (Stream<T>) searchResult.getDocuments().stream().map(Document::getId).map(key -> key.substring(idBegin));
} else {
resolvedStream = (Stream<T>) Stream.empty();
}
} else {
boolean returningFullEntity = (returning.stream().anyMatch(foi -> foi.getSearchAlias().equalsIgnoreCase("__this")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2431,4 +2431,14 @@
assertThat(d.getId()).isNotNull();
});
}

@Test void testMapAgainstEmptyResults() {
List<String> names = entityStream //
.of(Company.class) //
.filter(Company$.NAME.startsWith("Open"))
.map(Company$.ID)
.collect(Collectors.toList());

assertThat(names).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1520,4 +1520,14 @@
List<String> names = companies.stream().map(Company::getName).collect(Collectors.toList());
assertThat(names).contains("RedisInc");
}

@Test void testMapAgainstEmptyResults() {
List<String> names = entityStream //
.of(Company.class) //
.filter(Company$.NAME.startsWith("Open"))
.map(Company$.ID)
.collect(Collectors.toList());

assertThat(names).isEmpty();
}
}

0 comments on commit 4d087fe

Please sign in to comment.