Skip to content

Commit 7bcfc36

Browse files
authored
Merge pull request #923 from k163377/922-only-kotlin
Fixed hasRequiredMarker to only process content defined in Kotlin
2 parents e8f72f0 + dba7a14 commit 7bcfc36

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

release-notes/CREDITS-2.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Contributors:
1818
# 2.18.4 (not yet released)
1919

2020
WrongWrong (@k163377)
21+
* #923: Fixed hasRequiredMarker to only process content defined in Kotlin
2122
* #920: Minor refactors that do not affect behavior
2223

2324
# 2.18.3 (28-Feb-2025)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Co-maintainers:
1818

1919
2.18.4 (not yet released)
2020

21+
#923: Fixed a problem where the result of processing `hasRequiredMarker ` by a `KotlinModule` would also apply to
22+
classes defined in `Java` when `NullToEmptyCollection` or `NullToEmptyMap` was enabled.
2123
#920: Minor refactorings were made that did not affect behavior.
2224

2325
2.18.3 (28-Feb-2025)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ internal class KotlinAnnotationIntrospector(
3636
// TODO: implement nullIsSameAsDefault flag, which represents when TRUE that if something has a default value, it can be passed a null to default it
3737
// this likely impacts this class to be accurate about what COULD be considered required
3838

39-
override fun hasRequiredMarker(m: AnnotatedMember): Boolean? {
40-
val hasRequired = cache.javaMemberIsRequired(m) {
39+
override fun hasRequiredMarker(
40+
m: AnnotatedMember
41+
): Boolean? = m.takeIf { it.member.declaringClass.isKotlinClass() }?.let { _ ->
42+
cache.javaMemberIsRequired(m) {
4143
try {
4244
when {
4345
nullToEmptyCollection && m.type.isCollectionLikeType -> false
@@ -54,7 +56,6 @@ internal class KotlinAnnotationIntrospector(
5456
null
5557
}
5658
}
57-
return hasRequired
5859
}
5960

6061
override fun findSerializationConverter(a: Annotated): Converter<*, *>? = when (a) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fasterxml.jackson.module.kotlin.test.github
2+
3+
import com.fasterxml.jackson.databind.BeanDescription
4+
import com.fasterxml.jackson.databind.ObjectMapper
5+
import com.fasterxml.jackson.module.kotlin.KotlinFeature
6+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
7+
import kotlin.test.Test
8+
import kotlin.test.assertTrue
9+
10+
class GitHub922 {
11+
private inline fun <reified T : Any> ObjectMapper.introspectSerialization(): BeanDescription =
12+
serializationConfig.introspect(serializationConfig.constructType(T::class.java))
13+
14+
private inline fun <reified T : Any> ObjectMapper.introspectDeserialization(): BeanDescription =
15+
deserializationConfig.introspect(deserializationConfig.constructType(T::class.java))
16+
17+
private fun BeanDescription.isRequired(propertyName: String): Boolean =
18+
this.findProperties().first { it.name == propertyName }.isRequired
19+
20+
@Test
21+
fun `nullToEmpty does not override specification by Java annotation`() {
22+
val mapper = jacksonObjectMapper {
23+
enable(KotlinFeature.NullToEmptyCollection)
24+
enable(KotlinFeature.NullToEmptyMap)
25+
}
26+
27+
val desc = mapper.introspectDeserialization<GitHub922RequiredCollectionsDtoJava>()
28+
29+
assertTrue(desc.isRequired("list"))
30+
assertTrue(desc.isRequired("map"))
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fasterxml.jackson.module.kotlin.test.github;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class GitHub922RequiredCollectionsDtoJava {
10+
private final List<String> list;
11+
private final Map<String, String> map;
12+
13+
@JsonCreator
14+
public GitHub922RequiredCollectionsDtoJava(
15+
@JsonProperty(value = "list", required = true) List<String> list,
16+
@JsonProperty(value = "map", required = true) Map<String, String> map
17+
) {
18+
this.list = list;
19+
this.map = map;
20+
}
21+
22+
public List<String> getList() {
23+
return list;
24+
}
25+
26+
public Map<String, String> getMap() {
27+
return map;
28+
}
29+
}

0 commit comments

Comments
 (0)