fix(backend): reject whitespace-only person search queries
A query of only spaces previously fell through to findAllWithDocumentCount, exposing the full person list. Whitespace-only queries now return empty. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -24,10 +24,13 @@ public class PersonService {
|
|||||||
private final PersonRepository personRepository;
|
private final PersonRepository personRepository;
|
||||||
|
|
||||||
public List<PersonSummaryDTO> findAll(String q) {
|
public List<PersonSummaryDTO> findAll(String q) {
|
||||||
if (q != null && !q.isBlank()) {
|
if (q == null) {
|
||||||
return personRepository.searchWithDocumentCount(q);
|
return personRepository.findAllWithDocumentCount();
|
||||||
}
|
}
|
||||||
return personRepository.findAllWithDocumentCount();
|
if (q.isBlank()) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
return personRepository.searchWithDocumentCount(q.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person getById(UUID id) {
|
public Person getById(UUID id) {
|
||||||
|
|||||||
@@ -62,12 +62,9 @@ class PersonServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void findAll_returnsAll_whenQueryIsBlank() {
|
void findAll_returnsEmpty_whenQueryIsWhitespaceOnly() {
|
||||||
List<PersonSummaryDTO> expected = List.of();
|
assertThat(personService.findAll(" ")).isEmpty();
|
||||||
when(personRepository.findAllWithDocumentCount()).thenReturn(expected);
|
verify(personRepository, never()).findAllWithDocumentCount();
|
||||||
|
|
||||||
assertThat(personService.findAll(" ")).isEqualTo(expected);
|
|
||||||
verify(personRepository).findAllWithDocumentCount();
|
|
||||||
verify(personRepository, never()).searchWithDocumentCount(any());
|
verify(personRepository, never()).searchWithDocumentCount(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user