From 8b677b8faef92855da0e73d14f0e09d6e2f94daa Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Mon, 28 Apr 2025 16:22:31 +0900 Subject: [PATCH] Added tests for integration of isRequired granted to various accessors for https://github.com/FasterXML/jackson-module-kotlin/issues/668 --- .../module/kotlin/test/github/GitHub668.kt | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/test/kotlin/tools/jackson/module/kotlin/test/github/GitHub668.kt diff --git a/src/test/kotlin/tools/jackson/module/kotlin/test/github/GitHub668.kt b/src/test/kotlin/tools/jackson/module/kotlin/test/github/GitHub668.kt new file mode 100644 index 00000000..33ef0abb --- /dev/null +++ b/src/test/kotlin/tools/jackson/module/kotlin/test/github/GitHub668.kt @@ -0,0 +1,46 @@ +package tools.jackson.module.kotlin.test.github + +import com.fasterxml.jackson.annotation.JsonProperty +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import tools.jackson.databind.BeanDescription +import tools.jackson.databind.ObjectMapper +import tools.jackson.module.kotlin.defaultMapper +import tools.jackson.module.kotlin.jacksonObjectMapper + +class GitHub668 { + private inline fun ObjectMapper.introspectDeserialization(): BeanDescription = + _deserializationContext().introspectBeanDescription(_deserializationContext().constructType(T::class.java)) + + private fun BeanDescription.isRequired(propertyName: String): Boolean = + this.findProperties().find { it.name == propertyName }?.isRequired ?: false + + data class AffectByAccessor( + @get:JsonProperty(required = true) + var a: String?, + @field:JsonProperty(required = true) + var b: String?, + @set:JsonProperty(required = true) + var c: String? + ) { + @get:JsonProperty(required = true) + var x: String? = null + @field:JsonProperty(required = true) + var y: String? = null + @JvmField + @field:JsonProperty(required = true) + var z: String? = null + } + + @Test + fun affectByAccessorTestDeser() { + val desc = defaultMapper.introspectDeserialization() + + assertTrue(desc.isRequired("a")) + assertTrue(desc.isRequired("b")) + assertTrue(desc.isRequired("c")) + assertTrue(desc.isRequired("x")) + assertTrue(desc.isRequired("y")) + assertTrue(desc.isRequired("z")) + } +}