From b7cc1a926a292bd96dc8bbc1e2c662c54e8b67e0 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 10 Dec 2023 12:51:33 +0900 Subject: [PATCH] Make nulls = FAIL work even for primitives and missing Fixed bug by #738 --- .../module/kotlin/KotlinValueInstantiator.kt | 2 ++ .../module/kotlin/test/github/Github738.kt | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt index bc167abc1..5c37848b2 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinValueInstantiator.kt @@ -78,6 +78,8 @@ internal class KotlinValueInstantiator( paramDef.isOptional -> return@forEachIndexed // do not try to create any object if it is nullable and the value is missing paramType.isMarkedNullable -> null + // Primitive types always try to get from a buffer, considering several settings + jsonProp.type.isPrimitive -> buffer.getParameter(jsonProp) // to get suitable "missing" value provided by deserializer else -> jsonProp.valueDeserializer?.getAbsentValue(ctxt) } diff --git a/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt new file mode 100644 index 000000000..348a8fd66 --- /dev/null +++ b/src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github738.kt @@ -0,0 +1,20 @@ +package com.fasterxml.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.annotation.JsonSetter +import com.fasterxml.jackson.annotation.Nulls +import com.fasterxml.jackson.databind.exc.MismatchedInputException +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue +import org.junit.Assert.assertThrows +import org.junit.Test + +class Github738 { + data class D(@JsonSetter(nulls = Nulls.FAIL) val v: Int) + + @Test + fun test() { + val mapper = jacksonObjectMapper() + // nulls = FAIL is reflected if it is primitive and missing + assertThrows(MismatchedInputException::class.java) { mapper.readValue("{}") } + } +}