fix(api): skip physical books in metadata manager to prevent NPE (#234)

Physical books have no associated file, so calling getPrimaryBookFile()
returns null. The metadata manager was crashing with NullPointerException
when merging/consolidating metadata (authors, genres, etc.) that included
physical books.

This fix skips physical books in writeMetadataToFile() since there is no
file to write metadata to.

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Riccardo Casazza
2026-04-22 17:26:55 -04:00
committed by Zach Yale
co-authored by Claude Opus 4.5
parent 2474cdd4ae
commit ebdcd0bc19
2 changed files with 34 additions and 0 deletions
@@ -59,6 +59,9 @@ public class MetadataManagementService {
for (BookMetadataEntity metadata : metadataList) {
if (metadata.getBook() != null) {
BookEntity book = metadata.getBook();
if (Boolean.TRUE.equals(book.getIsPhysical())) {
continue;
}
boolean bookModified = false;
var primaryFile = book.getPrimaryBookFile();
@@ -592,4 +592,35 @@ class MetadataManagementServiceTest {
verify(metadataWriterFactory, never()).getWriter(any());
verify(authorRepository).delete(oldAuthor);
}
@Test
void consolidateAuthors_physicalBook_skipsFileWriteAndDoesNotThrow() {
AuthorEntity oldAuthor = AuthorEntity.builder().id(1L).name("Old").build();
AuthorEntity targetAuthor = AuthorEntity.builder().id(2L).name("Target").build();
when(authorRepository.findByNameIgnoreCase("Target")).thenReturn(Optional.of(targetAuthor));
when(authorRepository.save(targetAuthor)).thenReturn(targetAuthor);
when(authorRepository.findByNameIgnoreCase("Old")).thenReturn(Optional.of(oldAuthor));
BookEntity physicalBook = BookEntity.builder()
.id(1L)
.isPhysical(true)
.bookFiles(new ArrayList<>())
.build();
BookMetadataEntity metadata = BookMetadataEntity.builder()
.authors(new ArrayList<>(List.of(oldAuthor)))
.book(physicalBook)
.build();
physicalBook.setMetadata(metadata);
when(bookMetadataRepository.findAllByAuthorsContaining(oldAuthor)).thenReturn(List.of(metadata));
service.consolidateMetadata(MergeMetadataType.authors, List.of("Target"), List.of("Old"));
assertThat(metadata.getAuthors()).contains(targetAuthor);
assertThat(metadata.getAuthors()).doesNotContain(oldAuthor);
verify(metadataWriterFactory, never()).getWriter(any());
verify(bookRepository, never()).saveAndFlush(any());
verify(authorRepository).delete(oldAuthor);
}
}