From 401ebc73b6d3efab085268b869bed0d83e75219d Mon Sep 17 00:00:00 2001 From: junsu Date: Sun, 1 Jan 2023 01:34:49 +0900 Subject: [PATCH] refactor(web): Enhance AbstractKotlinSerializationHttpMessageConverter.serializer method * Add serializerInternal abstract method to extend serializer creation * Implement serializerInternal abstract method in KotlinSerializationBinaryHttpMessageConverter, KotlinSerializationJsonHttpMessageConverter --- .../AbstractKotlinSerializationHttpMessageConverter.java | 8 +++++++- .../KotlinSerializationBinaryHttpMessageConverter.java | 7 +++++++ .../KotlinSerializationJsonHttpMessageConverter.java | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/http/converter/AbstractKotlinSerializationHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/AbstractKotlinSerializationHttpMessageConverter.java index 8f047499e0f6..c4e7a4909dbd 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/AbstractKotlinSerializationHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/AbstractKotlinSerializationHttpMessageConverter.java @@ -152,7 +152,7 @@ private KSerializer serializer(Type type) { KSerializer serializer = serializerCache.get(type); if (serializer == null) { try { - serializer = SerializersKt.serializerOrNull(type); + serializer = serializerInternal(type); } catch (IllegalArgumentException ignored) { } @@ -166,6 +166,12 @@ private KSerializer serializer(Type type) { return serializer; } + /** + * An abstract method that returns a KSerializer over the given type. + */ + @Nullable + protected abstract KSerializer serializerInternal(Type type) throws IllegalArgumentException; + private boolean hasPolymorphism(SerialDescriptor descriptor, Set alreadyProcessed) { alreadyProcessed.add(descriptor.getSerialName()); if (descriptor.getKind().equals(PolymorphicKind.OPEN.INSTANCE)) { diff --git a/spring-web/src/main/java/org/springframework/http/converter/KotlinSerializationBinaryHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/KotlinSerializationBinaryHttpMessageConverter.java index bc094e9fe4b2..fde7ca5c2293 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/KotlinSerializationBinaryHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/KotlinSerializationBinaryHttpMessageConverter.java @@ -17,11 +17,13 @@ package org.springframework.http.converter; import java.io.IOException; +import java.lang.reflect.Type; import kotlinx.serialization.BinaryFormat; import kotlinx.serialization.KSerializer; import kotlinx.serialization.SerializationException; +import kotlinx.serialization.SerializersKt; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; @@ -76,4 +78,9 @@ protected void writeInternal(Object object, KSerializer serializer, T fo throw new HttpMessageNotWritableException("Could not write " + format + ": " + ex.getMessage(), ex); } } + + @Override + protected KSerializer serializerInternal(Type type) throws IllegalArgumentException { + return SerializersKt.serializerOrNull(type); + } } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java index bb47542d607a..a011ff594741 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java @@ -16,11 +16,15 @@ package org.springframework.http.converter.json; +import kotlinx.serialization.KSerializer; +import kotlinx.serialization.SerializersKt; import kotlinx.serialization.json.Json; import org.springframework.http.MediaType; import org.springframework.http.converter.KotlinSerializationStringHttpMessageConverter; +import java.lang.reflect.Type; + /** * Implementation of {@link org.springframework.http.converter.HttpMessageConverter} * that can read and write JSON using @@ -53,4 +57,9 @@ public KotlinSerializationJsonHttpMessageConverter() { public KotlinSerializationJsonHttpMessageConverter(Json json) { super(json, MediaType.APPLICATION_JSON, new MediaType("application", "*+json")); } + + @Override + protected KSerializer serializerInternal(Type type) throws IllegalArgumentException { + return SerializersKt.serializerOrNull(type); + } }