Skip to content

Commit a970fc1

Browse files
committed
Support properties in kotlinx.serialization codecs
This commit adds support for Kotlin properties in Spring WebFlux controllers, supported for reasons explained in gh-31856, with kotlinx.serialization codecs. See gh-34284
1 parent 683733a commit a970fc1

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java

+16-15
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,25 @@ protected final KSerializer<Object> serializer(ResolvableType resolvableType) {
133133
Assert.notNull(method, "Method must not be null");
134134
if (KotlinDetector.isKotlinType(method.getDeclaringClass())) {
135135
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
136-
Assert.notNull(function, "Kotlin function must not be null");
137-
KType type = (parameter.getParameterIndex() == -1 ? function.getReturnType() :
138-
KCallables.getValueParameters(function).get(parameter.getParameterIndex()).getType());
139-
KSerializer<Object> serializer = this.kTypeSerializerCache.get(type);
140-
if (serializer == null) {
141-
try {
142-
serializer = SerializersKt.serializerOrNull(this.format.getSerializersModule(), type);
143-
}
144-
catch (IllegalArgumentException ignored) {
145-
}
146-
if (serializer != null) {
147-
if (hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) {
148-
return null;
136+
if (function != null) {
137+
KType type = (parameter.getParameterIndex() == -1 ? function.getReturnType() :
138+
KCallables.getValueParameters(function).get(parameter.getParameterIndex()).getType());
139+
KSerializer<Object> serializer = this.kTypeSerializerCache.get(type);
140+
if (serializer == null) {
141+
try {
142+
serializer = SerializersKt.serializerOrNull(this.format.getSerializersModule(), type);
143+
}
144+
catch (IllegalArgumentException ignored) {
145+
}
146+
if (serializer != null) {
147+
if (hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) {
148+
return null;
149+
}
150+
this.kTypeSerializerCache.put(type, serializer);
149151
}
150-
this.kTypeSerializerCache.put(type, serializer);
151152
}
153+
return serializer;
152154
}
153-
return serializer;
154155
}
155156
}
156157
Type type = resolvableType.getType();

spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonEncoderTests.kt

+14
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,24 @@ class KotlinSerializationJsonEncoderTests : AbstractEncoderTests<KotlinSerializa
145145
assertThat(encoder.canEncode(ResolvableType.forClass(BigDecimal::class.java), null)).isFalse()
146146
}
147147

148+
@Test
149+
fun encodeProperty() {
150+
val input = Mono.just(value)
151+
val method = this::class.java.getDeclaredMethod("getValue")
152+
val methodParameter = MethodParameter.forExecutable(method, -1)
153+
testEncode(input, ResolvableType.forMethodParameter(methodParameter), null, null) {
154+
it.consumeNextWith(expectString("42"))
155+
.verifyComplete()
156+
}
157+
}
158+
148159

149160
@Serializable
150161
data class Pojo(val foo: String, val bar: String, val pojo: Pojo? = null)
151162

152163
fun handleMapWithNullable(map: Map<String, String?>) = map
153164

165+
val value: Int
166+
get() = 42
167+
154168
}

0 commit comments

Comments
 (0)