fix: improper json deserialization when missing mandatory fields

This commit is contained in:
Gauthier Roebroeck 2022-01-10 13:47:06 +08:00
parent ebf94cbeea
commit 594194fafd
5 changed files with 99 additions and 4 deletions

View file

@ -43,6 +43,9 @@ spring:
web:
resources:
add-mappings: false
jackson:
deserialization:
FAIL_ON_NULL_FOR_PRIMITIVES: true
server:
servlet.session.timeout: 7d

View file

@ -1,15 +1,23 @@
package org.gotson.komga.infrastructure.metadata.mylar.dto
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.catchThrowable
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.core.io.ClassPathResource
import org.springframework.test.context.junit.jupiter.SpringExtension
class SeriesTest {
private val mapper = ObjectMapper().registerKotlinModule()
@ExtendWith(SpringExtension::class)
@SpringBootTest
class SeriesTest(
@Autowired private val mapper: ObjectMapper,
) {
@Test
fun `given valid json file when deserializing then properties are available`() {
@ -60,4 +68,28 @@ class SeriesTest {
assertThat(status).isEqualTo(Status.Ended)
}
}
@Test
fun `given invalid json file missing year when deserializing then it fails`() {
val file = ClassPathResource("mylar/series-missing-year.json")
val thrown = catchThrowable { mapper.readValue<Series>(file.url) }
assertThat(thrown).isInstanceOf(MismatchedInputException::class.java)
}
@Test
fun `given invalid json file missing publisher when deserializing then it fails`() {
val file = ClassPathResource("mylar/series-missing-publisher.json")
val thrown = catchThrowable { mapper.readValue<Series>(file.url) }
assertThat(thrown).isInstanceOf(MissingKotlinParameterException::class.java)
}
@Test
fun `given invalid json file missing status when deserializing then it fails`() {
val file = ClassPathResource("mylar/series-missing-status.json")
val thrown = catchThrowable { mapper.readValue<Series>(file.url) }
assertThat(thrown).isInstanceOf(MissingKotlinParameterException::class.java)
}
}

View file

@ -0,0 +1,20 @@
{
"version": "1.0.1",
"metadata": {
"type": "comicSeries",
"imprint": null,
"name": "Usagi Yojimbo",
"cid": 119731,
"year": 2019,
"description_text": null,
"description_formatted": null,
"volume": 4,
"booktype": "Print",
"age_rating": null,
"collects": null,
"ComicImage": "https://comicvine1.cbsistatic.com/uploads/scale_large/6/67663/6974029-01a.jpg",
"total_issues": 20,
"publication_run": "June 2019 - Present",
"status": "Ended"
}
}

View file

@ -0,0 +1,20 @@
{
"version": "1.0.1",
"metadata": {
"type": "comicSeries",
"publisher": "IDW Publishing",
"imprint": null,
"name": "Usagi Yojimbo",
"cid": 119731,
"year": 2019,
"description_text": null,
"description_formatted": null,
"volume": 4,
"booktype": "Print",
"age_rating": null,
"collects": null,
"ComicImage": "https://comicvine1.cbsistatic.com/uploads/scale_large/6/67663/6974029-01a.jpg",
"total_issues": 20,
"publication_run": "June 2019 - Present"
}
}

View file

@ -0,0 +1,20 @@
{
"version": "1.0.1",
"metadata": {
"type": "comicSeries",
"publisher": "IDW Publishing",
"imprint": null,
"name": "Usagi Yojimbo",
"cid": 119731,
"description_text": null,
"description_formatted": null,
"volume": 4,
"booktype": "Print",
"age_rating": null,
"collects": null,
"ComicImage": "https://comicvine1.cbsistatic.com/uploads/scale_large/6/67663/6974029-01a.jpg",
"total_issues": 20,
"publication_run": "June 2019 - Present",
"status": "Ended"
}
}