Skip to content

Commit

Permalink
Merge pull request #868 from k163377/add-null-for-primitive-test
Browse files Browse the repository at this point in the history
Added test case for FAIL_ON_NULL_FOR_PRIMITIVES
  • Loading branch information
k163377 authored Dec 15, 2024
2 parents 2ee6fe9 + a1ace2f commit d94e3d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Contributors:
# 2.19.0 (not yet released)

WrongWrong (@k163377)
* #868: Added test case for FAIL_ON_NULL_FOR_PRIMITIVES
* #866: Upgrade to JUnit5
* #861: Update Kotlin to 1.9.24
* #858: Refactor findDefaultCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,47 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test

class FailNullForPrimitiveTest {
data class Dto(
val mapper = jacksonObjectMapper()
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)

data class NoDefaultValue(
val foo: Int,
val bar: Int?
)

@Test
fun test() {
val mapper = jacksonObjectMapper()
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
fun noDefaultValueTest() {
// If no default value is set, it will fail if undefined or null is entered
assertThrows(MismatchedInputException::class.java) {
mapper.readValue<NoDefaultValue>("{}")
}

assertThrows(MismatchedInputException::class.java) {
mapper.readValue<Dto>("{}")
mapper.readValue<NoDefaultValue>("""{"foo":null}""")
}

assertEquals(NoDefaultValue(0, null), mapper.readValue<NoDefaultValue>("""{"foo":0}"""))
}

data class HasDefaultValue(
val foo: Int = -1,
val bar: Int? = -1
)

@Test
fun hasDefaultValueTest() {
// If a default value is set, an input of undefined will succeed, but null will fail
assertEquals(HasDefaultValue(-1, -1), mapper.readValue<HasDefaultValue>("{}"))

assertThrows(MismatchedInputException::class.java) {
mapper.readValue<Dto>("""{"foo":null}""")
mapper.readValue<HasDefaultValue>("""{"foo":null}""")
}

assertEquals(Dto(0, null), mapper.readValue<Dto>("""{"foo":0}"""))
assertEquals(HasDefaultValue(0, null), mapper.readValue<HasDefaultValue>("""{"foo":0, "bar":null}"""))
}
}

0 comments on commit d94e3d9

Please sign in to comment.