Skip to content

Commit 88ac39b

Browse files
authored
Merge pull request #17 from ProjectMapK/set-only-kotlin-property
Set only kotlin property
2 parents 158f783 + 6ab899e commit 88ac39b

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinAnnotationIntrospector.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import java.lang.reflect.Constructor
2626
import java.lang.reflect.Field
2727
import java.lang.reflect.Method
2828
import kotlin.reflect.KFunction
29-
import kotlin.reflect.KType
30-
import kotlin.reflect.full.createType
3129
import kotlin.reflect.jvm.javaType
3230
import kotlin.reflect.jvm.kotlinFunction
3331

@@ -159,7 +157,6 @@ internal class KotlinAnnotationIntrospector(
159157
// This could be a setter or a getter of a class property or
160158
// a setter-like/getter-like method.
161159
private fun AnnotatedMethod.hasRequiredMarker(): Boolean? = this.getRequiredMarkerFromCorrespondingAccessor()
162-
?: this.member.getRequiredMarkerFromAccessorLikeMethod()
163160

164161
private fun AnnotatedMethod.getRequiredMarkerFromCorrespondingAccessor(): Boolean? {
165162
val memberSignature = member.toSignature()
@@ -173,17 +170,6 @@ internal class KotlinAnnotationIntrospector(
173170
return null
174171
}
175172

176-
// Is the member method a regular method of the data class or
177-
private fun Method.getRequiredMarkerFromAccessorLikeMethod(): Boolean? = this.kotlinFunction?.let { method ->
178-
val byAnnotation = this.isRequiredByAnnotation()
179-
return when {
180-
method.isSetterLike() -> requiredAnnotationOrNullability(byAnnotation, method.isMethodParameterRequired(0))
181-
else -> null
182-
}
183-
}
184-
185-
private fun KFunction<*>.isSetterLike(): Boolean = parameters.size == 2 && returnType == UNIT_TYPE
186-
187173
private fun AnnotatedParameter.hasRequiredMarker(): Boolean? {
188174
val member = this.member
189175
val byAnnotation = this.getAnnotation(JsonProperty::class.java)?.required
@@ -217,8 +203,4 @@ internal class KotlinAnnotationIntrospector(
217203
return !paramType.isMarkedNullable && !param.isOptional &&
218204
!(isPrimitive && !context.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES))
219205
}
220-
221-
companion object {
222-
val UNIT_TYPE: KType = Unit::class.createType()
223-
}
224206
}

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinNamesAnnotationIntrospector.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.introspect.AnnotatedParameter
1111
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector
1212
import kotlinx.metadata.jvm.fieldSignature
1313
import kotlinx.metadata.jvm.getterSignature
14+
import kotlinx.metadata.jvm.setterSignature
1415
import java.lang.reflect.Constructor
1516
import java.lang.reflect.Method
1617
import kotlin.reflect.KClass
@@ -59,6 +60,14 @@ internal class KotlinNamesAnnotationIntrospector(val module: KotlinModule, val c
5960
else -> null
6061
}
6162

63+
override fun hasIgnoreMarker(m: AnnotatedMember): Boolean = (m as? AnnotatedMethod)?.member
64+
?.takeIf { it.parameters.size == 1 /* && it.returnType == Void::class.java */ }
65+
?.let { it.declaringClass.toKmClass() }
66+
?.let { kmClass ->
67+
val methodSignature = m.annotated.toSignature()
68+
kmClass.properties.none { it.setterSignature == methodSignature }
69+
} ?: false
70+
6271
@Suppress("UNCHECKED_CAST")
6372
private fun hasCreatorAnnotation(member: AnnotatedConstructor): Boolean {
6473
// don't add a JsonCreator to any constructor if one is declared already

src/test/kotlin/com/fasterxml/jackson/module/kotlin/_ported/test/PropertyRequirednessTests.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class TestPropertyRequiredness {
5050
"h".isOptionalForSerializationOf(testClass, mapper)
5151
"h".isOptionalForDeserializationOf(testClass, mapper)
5252

53-
"i".isRequiredForDeserializationOf(testClass, mapper)
53+
"i".isOptionalForDeserializationOf(testClass, mapper)
5454
"j".isOptionalForDeserializationOf(testClass, mapper)
5555
"k".isOptionalForDeserializationOf(testClass, mapper)
5656
"l".isOptionalForDeserializationOf(testClass, mapper)
@@ -59,7 +59,7 @@ class TestPropertyRequiredness {
5959
@Test fun shouldHandleTrueFailOnNullForPrimitives() {
6060
val mapper = jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true)
6161
val testClass = TestClass::class.java
62-
"a".isRequiredForDeserializationOf(testClass, mapper)
62+
"a".isOptionalForDeserializationOf(testClass, mapper)
6363
"b".isOptionalForDeserializationOf(testClass, mapper)
6464
"c".isOptionalForDeserializationOf(testClass, mapper)
6565
"d".isOptionalForDeserializationOf(testClass, mapper)
@@ -68,7 +68,7 @@ class TestPropertyRequiredness {
6868

6969
"h".isOptionalForDeserializationOf(testClass, mapper)
7070

71-
"i".isRequiredForDeserializationOf(testClass, mapper)
71+
"i".isOptionalForDeserializationOf(testClass, mapper)
7272
"j".isOptionalForDeserializationOf(testClass, mapper)
7373
"k".isOptionalForDeserializationOf(testClass, mapper)
7474
"l".isOptionalForDeserializationOf(testClass, mapper)
@@ -194,5 +194,5 @@ class TestPropertyRequiredness {
194194
mapper.deserializationConfig.introspect(mapper.deserializationConfig.constructType(type))
195195

196196
private fun BeanDescription.isRequired(propertyName: String): Boolean =
197-
this.findProperties().find { it.name == propertyName }!!.isRequired
197+
this.findProperties().find { it.name == propertyName }?.isRequired ?: false
198198
}

src/test/kotlin/com/fasterxml/jackson/module/kotlin/_ported/test/github/Github165.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.fasterxml.jackson.module.kotlin._ported.test.github
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore
34
import com.fasterxml.jackson.annotation.JsonProperty
4-
import com.fasterxml.jackson.annotation.JsonSetter
55
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
66
import com.fasterxml.jackson.module.kotlin.readValue
77
import com.fasterxml.jackson.module.kotlin.test.github._ported.Github165JavaTest
@@ -15,20 +15,21 @@ class TestGithub165 {
1515
var yearSetterCalled: Boolean = false
1616
var nameSetterCalled: Boolean = false
1717

18-
@JsonProperty("year")
19-
lateinit var showYear: String
18+
var showYear: String? = null
19+
@JsonProperty("year")
20+
set(value) {
21+
yearSetterCalled = true
22+
field = value
23+
}
2024

21-
@JsonSetter("year")
22-
fun setYear(value: String) {
23-
yearSetterCalled = true
24-
this.showYear = value
25-
}
25+
var name: String
26+
@JsonIgnore get() = showName // Why define get: https://youtrack.jetbrains.com/issue/KT-6519
2627

27-
@JsonSetter("name")
28-
fun setName(value: String) {
29-
nameSetterCalled = true
30-
this.showName = value
31-
}
28+
@JsonProperty("name")
29+
set(value) {
30+
nameSetterCalled = true
31+
this.showName = value
32+
}
3233
}
3334

3435
@Test

src/test/kotlin/com/fasterxml/jackson/module/kotlin/_ported/test/github/Github308.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class TestGithub308 {
1717
var id: Long? = null,
1818
var cityId: Int? = null
1919
) {
20-
@JsonProperty("id")
21-
private fun unpackId(idObj: Int?) {
22-
cityId = idObj
23-
}
20+
private var unpackId: Int?
21+
get() = cityId // Why define get: https://youtrack.jetbrains.com/issue/KT-6519
22+
23+
@JsonProperty("id")
24+
set(value) { cityId = value }
2425
}
2526

2627
@Test

0 commit comments

Comments
 (0)