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
This commit is contained in:
@@ -133,24 +133,25 @@ public abstract class KotlinSerializationSupport<T extends SerialFormat> {
|
|||||||
Assert.notNull(method, "Method must not be null");
|
Assert.notNull(method, "Method must not be null");
|
||||||
if (KotlinDetector.isKotlinType(method.getDeclaringClass())) {
|
if (KotlinDetector.isKotlinType(method.getDeclaringClass())) {
|
||||||
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
|
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
|
||||||
Assert.notNull(function, "Kotlin function must not be null");
|
if (function != null) {
|
||||||
KType type = (parameter.getParameterIndex() == -1 ? function.getReturnType() :
|
KType type = (parameter.getParameterIndex() == -1 ? function.getReturnType() :
|
||||||
KCallables.getValueParameters(function).get(parameter.getParameterIndex()).getType());
|
KCallables.getValueParameters(function).get(parameter.getParameterIndex()).getType());
|
||||||
KSerializer<Object> serializer = this.kTypeSerializerCache.get(type);
|
KSerializer<Object> serializer = this.kTypeSerializerCache.get(type);
|
||||||
if (serializer == null) {
|
if (serializer == null) {
|
||||||
try {
|
try {
|
||||||
serializer = SerializersKt.serializerOrNull(this.format.getSerializersModule(), type);
|
serializer = SerializersKt.serializerOrNull(this.format.getSerializersModule(), type);
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ignored) {
|
catch (IllegalArgumentException ignored) {
|
||||||
}
|
}
|
||||||
if (serializer != null) {
|
if (serializer != null) {
|
||||||
if (hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) {
|
if (hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
this.kTypeSerializerCache.put(type, serializer);
|
||||||
}
|
}
|
||||||
this.kTypeSerializerCache.put(type, serializer);
|
|
||||||
}
|
}
|
||||||
|
return serializer;
|
||||||
}
|
}
|
||||||
return serializer;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Type type = resolvableType.getType();
|
Type type = resolvableType.getType();
|
||||||
|
|||||||
@@ -145,10 +145,24 @@ class KotlinSerializationJsonEncoderTests : AbstractEncoderTests<KotlinSerializa
|
|||||||
assertThat(encoder.canEncode(ResolvableType.forClass(BigDecimal::class.java), null)).isFalse()
|
assertThat(encoder.canEncode(ResolvableType.forClass(BigDecimal::class.java), null)).isFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun encodeProperty() {
|
||||||
|
val input = Mono.just(value)
|
||||||
|
val method = this::class.java.getDeclaredMethod("getValue")
|
||||||
|
val methodParameter = MethodParameter.forExecutable(method, -1)
|
||||||
|
testEncode(input, ResolvableType.forMethodParameter(methodParameter), null, null) {
|
||||||
|
it.consumeNextWith(expectString("42"))
|
||||||
|
.verifyComplete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Pojo(val foo: String, val bar: String, val pojo: Pojo? = null)
|
data class Pojo(val foo: String, val bar: String, val pojo: Pojo? = null)
|
||||||
|
|
||||||
fun handleMapWithNullable(map: Map<String, String?>) = map
|
fun handleMapWithNullable(map: Map<String, String?>) = map
|
||||||
|
|
||||||
|
val value: Int
|
||||||
|
get() = 42
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user