From 9218e6bb265b32fbd28d61ef797e7a1d93cb9a1c Mon Sep 17 00:00:00 2001 From: Gauthier Roebroeck Date: Mon, 23 Mar 2020 11:57:44 +0800 Subject: [PATCH] fix(api): check if authors field is set closes #120 --- .../komga/interfaces/rest/BookController.kt | 12 +++-- .../rest/dto/BookMetadataUpdateDto.kt | 7 ++- .../interfaces/rest/BookControllerTest.kt | 54 +++++++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt index 71fc19fca..7e6d7cc0e 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/BookController.kt @@ -364,11 +364,13 @@ class BookController( book.metadata.releaseDate = newMetadata.releaseDate } releaseDateLock?.let { book.metadata.releaseDateLock = it } - if (authors != null) { - book.metadata.authors = authors!!.map { - Author(it.name ?: "", it.role ?: "") - }.toMutableList() - } else book.metadata.authors = mutableListOf() + if (isSet("authors")) { + if (authors != null) { + book.metadata.authors = authors!!.map { + Author(it.name ?: "", it.role ?: "") + }.toMutableList() + } else book.metadata.authors = mutableListOf() + } authorsLock?.let { book.metadata.authorsLock = it } } bookRepository.save(book).toDto(includeFullUrl = true) diff --git a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/dto/BookMetadataUpdateDto.kt b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/dto/BookMetadataUpdateDto.kt index f3c4657df..4962f44b5 100644 --- a/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/dto/BookMetadataUpdateDto.kt +++ b/komga/src/main/kotlin/org/gotson/komga/interfaces/rest/dto/BookMetadataUpdateDto.kt @@ -56,8 +56,11 @@ class BookMetadataUpdateDto { var releaseDateLock: Boolean? = null - @Valid - var authors: List? = null + @get:Valid + var authors: List? + by Delegates.observable?>(null) { prop, _, _ -> + isSet[prop.name] = true + } var authorsLock: Boolean? = null } diff --git a/komga/src/test/kotlin/org/gotson/komga/interfaces/rest/BookControllerTest.kt b/komga/src/test/kotlin/org/gotson/komga/interfaces/rest/BookControllerTest.kt index 654b2a50a..3aacc98bc 100644 --- a/komga/src/test/kotlin/org/gotson/komga/interfaces/rest/BookControllerTest.kt +++ b/komga/src/test/kotlin/org/gotson/komga/interfaces/rest/BookControllerTest.kt @@ -524,4 +524,58 @@ class BookControllerTest( assertThat(releaseDate).isNull() } } + + //Not part of the above @Nested class because @Transactional fails + @Test + @Transactional + @WithMockCustomUser(roles = [UserRoles.ADMIN]) + fun `given json without fields when updating metadata then existing fields are untouched`() { + val testDate = LocalDate.of(2020, 1, 1) + val series = makeSeries( + name = "series", + books = listOf(makeBook("1.cbr").also { + with(it.metadata) + { + ageRating = 12 + readingDirection = BookMetadata.ReadingDirection.LEFT_TO_RIGHT + authors.add(Author("Author", "role")) + releaseDate = testDate + summary = "summary" + number = "number" + numberLock = true + numberSort = 2F + numberSortLock = true + publisher = "publisher" + title = "title" + } + }) + ).also { it.library = library } + seriesRepository.save(series) + val bookId = series.books.first().id + + val jsonString = """ + { + } + """.trimIndent() + + mockMvc.patch("/api/v1/books/${bookId}/metadata") { + contentType = MediaType.APPLICATION_JSON + content = jsonString + }.andExpect { + status { isOk } + } + + val updatedBook = bookRepository.findByIdOrNull(bookId) + with(updatedBook!!.metadata) { + assertThat(readingDirection).isEqualTo(BookMetadata.ReadingDirection.LEFT_TO_RIGHT) + assertThat(ageRating).isEqualTo(12) + assertThat(authors).hasSize(1) + assertThat(releaseDate).isEqualTo(testDate) + assertThat(summary).isEqualTo("summary") + assertThat(number).isEqualTo("number") + assertThat(numberSort).isEqualTo(2F) + assertThat(publisher).isEqualTo("publisher") + assertThat(title).isEqualTo("title") + } + } }