diff --git a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryDecoder.java
index 46d5e9f75b4..c123629434b 100644
--- a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryDecoder.java
+++ b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryDecoder.java
@@ -18,6 +18,7 @@ package org.springframework.http.codec;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
import kotlinx.serialization.BinaryFormat;
import kotlinx.serialization.KSerializer;
@@ -37,9 +38,11 @@ import org.springframework.util.MimeType;
* Abstract base class for {@link Decoder} implementations that defer to Kotlin
* {@linkplain BinaryFormat binary serializers}.
*
- *
As of Spring Framework 7.0,
- * open polymorphism
- * is supported.
+ *
As of Spring Framework 7.0, by default it only decodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or generics level
+ * since it allows combined usage with other general purpose decoders without conflicts.
+ * Alternative constructors with a {@code Predicate} parameter can be used
+ * to customize this behavior.
*
* @author Sebastien Deleuze
* @author Iain Henderson
@@ -54,10 +57,26 @@ public abstract class KotlinSerializationBinaryDecoder e
private final ByteArrayDecoder byteArrayDecoder = new ByteArrayDecoder();
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only decodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or
+ * generics level.
+ */
public KotlinSerializationBinaryDecoder(T format, MimeType... supportedMimeTypes) {
super(format, supportedMimeTypes);
}
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only decodes types for which the specified predicate returns
+ * {@code true}.
+ * @since 7.0
+ */
+ public KotlinSerializationBinaryDecoder(T format, Predicate typePredicate, MimeType... supportedMimeTypes) {
+ super(format, typePredicate, supportedMimeTypes);
+ }
+
/**
* Configure a limit on the number of bytes that can be buffered whenever
* the input stream needs to be aggregated. This can be a result of
diff --git a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryEncoder.java
index 3a3170b0e98..3b5f18d979d 100644
--- a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryEncoder.java
+++ b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationBinaryEncoder.java
@@ -18,6 +18,7 @@ package org.springframework.http.codec;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
import kotlinx.serialization.BinaryFormat;
import kotlinx.serialization.KSerializer;
@@ -38,9 +39,11 @@ import org.springframework.util.MimeType;
* Abstract base class for {@link Encoder} implementations that defer to Kotlin
* {@linkplain BinaryFormat binary serializers}.
*
- *
As of Spring Framework 7.0,
- * open polymorphism
- * is supported.
+ *
As of Spring Framework 7.0, by default it only encodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or generics level
+ * since it allows combined usage with other general purpose encoders without conflicts.
+ * Alternative constructors with a {@code Predicate} parameter can be used
+ * to customize this behavior.
*
* @author Sebastien Deleuze
* @author Iain Henderson
@@ -54,10 +57,26 @@ public abstract class KotlinSerializationBinaryEncoder e
// ByteArraySequence encoding needed for now, see https://github.com/Kotlin/kotlinx.serialization/issues/204 for more details
private final ByteArrayEncoder byteArrayEncoder = new ByteArrayEncoder();
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only encodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or
+ * generics level.
+ */
protected KotlinSerializationBinaryEncoder(T format, MimeType... supportedMimeTypes) {
super(format, supportedMimeTypes);
}
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only encodes types for which the specified predicate returns
+ * {@code true}.
+ * @since 7.0
+ */
+ protected KotlinSerializationBinaryEncoder(T format, Predicate typePredicate, MimeType... supportedMimeTypes) {
+ super(format, typePredicate, supportedMimeTypes);
+ }
+
@Override
public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType) {
return canSerialize(elementType, mimeType);
diff --git a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringDecoder.java
index 423aa4087cf..684df592298 100644
--- a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringDecoder.java
+++ b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringDecoder.java
@@ -18,6 +18,7 @@ package org.springframework.http.codec;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
import kotlinx.serialization.KSerializer;
import kotlinx.serialization.StringFormat;
@@ -38,9 +39,11 @@ import org.springframework.util.MimeType;
* Abstract base class for {@link Decoder} implementations that defer to Kotlin
* {@linkplain StringFormat string serializers}.
*
- *
As of Spring Framework 7.0,
- * open polymorphism
- * is supported.
+ *
As of Spring Framework 7.0, by default it only decodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or generics level
+ * since it allows combined usage with other general purpose decoders without conflicts.
+ * Alternative constructors with a {@code Predicate} parameter can be used
+ * to customize this behavior.
*
* @author Sebastien Deleuze
* @author Iain Henderson
@@ -55,10 +58,26 @@ public abstract class KotlinSerializationStringDecoder e
private final StringDecoder stringDecoder = StringDecoder.allMimeTypes(StringDecoder.DEFAULT_DELIMITERS, false);
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only decodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or
+ * generics level.
+ */
public KotlinSerializationStringDecoder(T format, MimeType... supportedMimeTypes) {
super(format, supportedMimeTypes);
}
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only decodes types for which the specified predicate returns
+ * {@code true}.
+ * @since 7.0
+ */
+ public KotlinSerializationStringDecoder(T format, Predicate typePredicate, MimeType... supportedMimeTypes) {
+ super(format, typePredicate, supportedMimeTypes);
+ }
+
/**
* Configure a limit on the number of bytes that can be buffered whenever
* the input stream needs to be aggregated. This can be a result of
diff --git a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringEncoder.java b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringEncoder.java
index b150ada67cc..18d7330b453 100644
--- a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringEncoder.java
+++ b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationStringEncoder.java
@@ -22,6 +22,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.function.Predicate;
import kotlinx.serialization.KSerializer;
import kotlinx.serialization.StringFormat;
@@ -43,9 +44,11 @@ import org.springframework.util.MimeType;
* Abstract base class for {@link Encoder} implementations that defer to Kotlin
* {@linkplain StringFormat string serializers}.
*
- *
As of Spring Framework 7.0,
- * open polymorphism
- * is supported.
+ *
As of Spring Framework 7.0, by default it only encodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or generics level
+ * since it allows combined usage with other general purpose encoders without conflicts.
+ * Alternative constructors with a {@code Predicate} parameter can be used
+ * to customize this behavior.
*
* @author Sebastien Deleuze
* @author Iain Henderson
@@ -65,10 +68,27 @@ public abstract class KotlinSerializationStringEncoder e
private final CharSequenceEncoder charSequenceEncoder = CharSequenceEncoder.allMimeTypes();
private final Set streamingMediaTypes = new HashSet<>();
+
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only encodes types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or
+ * generics level.
+ */
protected KotlinSerializationStringEncoder(T format, MimeType... supportedMimeTypes) {
super(format, supportedMimeTypes);
}
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only encodes types for which the specified predicate returns
+ * {@code true}.
+ * @since 7.0
+ */
+ protected KotlinSerializationStringEncoder(T format, Predicate typePredicate, MimeType... supportedMimeTypes) {
+ super(format, typePredicate, supportedMimeTypes);
+ }
+
/**
* Set streaming {@link MediaType MediaTypes}.
* @param streamingMediaTypes streaming {@link MediaType MediaTypes}
diff --git a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java
index 7744911da0e..1d144926c0a 100644
--- a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java
+++ b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java
@@ -21,6 +21,7 @@ import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
+import java.util.function.Predicate;
import kotlin.reflect.KFunction;
import kotlin.reflect.KType;
@@ -42,9 +43,11 @@ import org.springframework.util.MimeType;
* Base class providing support methods for encoding and decoding with Kotlin
* serialization.
*
- *
As of Spring Framework 7.0,
- * open polymorphism
- * is supported.
+ *
As of Spring Framework 7.0, by default it only handles types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or generics level
+ * since it allows combined usage with other general purpose decoders without conflicts.
+ * Alternative constructors with a {@code Predicate} parameter can be used
+ * to customize this behavior.
*
* @author Sebastien Deleuze
* @author Iain Henderson
@@ -63,12 +66,29 @@ public abstract class KotlinSerializationSupport {
private final List supportedMimeTypes;
+ private final Predicate typePredicate;
+
/**
- * Creates a new instance of this support class with the given format
- * and supported mime types.
+ * Creates a new instance with the given format and supported mime types
+ * which only handle types annotated with
+ * {@link kotlinx.serialization.Serializable @Serializable} at type or
+ * generics level.
*/
protected KotlinSerializationSupport(T format, MimeType... supportedMimeTypes) {
this.format = format;
+ this.typePredicate = KotlinDetector::hasSerializableAnnotation;
+ this.supportedMimeTypes = Arrays.asList(supportedMimeTypes);
+ }
+
+ /**
+ * Creates a new instance with the given format and supported mime types
+ * which only encode types for which the specified predicate returns
+ * {@code true}.
+ * @since 7.0
+ */
+ protected KotlinSerializationSupport(T format, Predicate typePredicate, MimeType... supportedMimeTypes) {
+ this.format = format;
+ this.typePredicate = typePredicate;
this.supportedMimeTypes = Arrays.asList(supportedMimeTypes);
}
@@ -94,15 +114,10 @@ public abstract class KotlinSerializationSupport {
* @return {@code true} if {@code type} can be serialized; false otherwise
*/
protected final boolean canSerialize(ResolvableType type, @Nullable MimeType mimeType) {
- KSerializer