Skip to content

Commit

Permalink
Fix a regression in Kotlin serialization support
Browse files Browse the repository at this point in the history
After spring-projectsgh-29068, some Type instances can throw an
IllegalArgumentException. This commit catches them
properly to just return a null serializer in that
case.

Closes spring-projectsgh-29192
  • Loading branch information
sdeleuze committed Sep 26, 2022
1 parent 94e5eb3 commit 42ab1b7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ public Mono<Object> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableTy
private KSerializer<Object> serializer(Type type) {
KSerializer<Object> serializer = serializerCache.get(type);
if (serializer == null) {
serializer = SerializersKt.serializerOrNull(type);
try {
serializer = SerializersKt.serializerOrNull(type);
}
catch (IllegalArgumentException ignored) {
}
if (serializer == null || hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory,
private KSerializer<Object> serializer(Type type) {
KSerializer<Object> serializer = serializerCache.get(type);
if (serializer == null) {
serializer = SerializersKt.serializerOrNull(type);
try {
serializer = SerializersKt.serializerOrNull(type);
}
catch (IllegalArgumentException ignored) {
}
if (serializer == null || hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ private Charset getCharsetToUse(@Nullable MediaType contentType) {
private KSerializer<Object> serializer(Type type) {
KSerializer<Object> serializer = serializerCache.get(type);
if (serializer == null) {
serializer = SerializersKt.serializerOrNull(type);
try {
serializer = SerializersKt.serializerOrNull(type);
}
catch (IllegalArgumentException ignored) {
}
if (serializer == null || hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class KotlinSerializationJsonDecoderTests : AbstractDecoderTests<KotlinSerializa
Assertions.assertThat(decoder.canDecode(ResolvableType.forClassWithGenerics(ArrayList::class.java, Int::class.java), MediaType.APPLICATION_JSON)).isTrue()
Assertions.assertThat(decoder.canDecode(ResolvableType.forClassWithGenerics(ArrayList::class.java, Int::class.java), MediaType.APPLICATION_PDF)).isFalse()
Assertions.assertThat(decoder.canDecode(ResolvableType.forClass(Ordered::class.java), MediaType.APPLICATION_JSON)).isFalse()
Assertions.assertThat(decoder.canDecode(ResolvableType.NONE, MediaType.APPLICATION_JSON)).isFalse()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class KotlinSerializationJsonEncoderTests : AbstractEncoderTests<KotlinSerializa
Assertions.assertThat(encoder.canEncode(ResolvableType.forClassWithGenerics(List::class.java, Pojo::class.java), MediaType.APPLICATION_JSON)).isTrue()
Assertions.assertThat(encoder.canEncode(ResolvableType.forClassWithGenerics(ArrayList::class.java, Int::class.java), MediaType.APPLICATION_JSON)).isTrue()
Assertions.assertThat(encoder.canEncode(ResolvableType.forClassWithGenerics(ArrayList::class.java, Int::class.java), MediaType.APPLICATION_PDF)).isFalse()
Assertions.assertThat(encoder.canEncode(ResolvableType.NONE, MediaType.APPLICATION_JSON)).isFalse()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.jupiter.api.Test
import org.springframework.core.Ordered
import org.springframework.core.ResolvableType
import org.springframework.http.MediaType
import org.springframework.http.MockHttpInputMessage
import org.springframework.http.MockHttpOutputMessage
Expand Down Expand Up @@ -62,6 +63,8 @@ class KotlinSerializationJsonHttpMessageConverterTests {

assertThat(converter.canRead(typeTokenOf<Ordered>(), Ordered::class.java, MediaType.APPLICATION_JSON)).isFalse()
assertThat(converter.canRead(typeTokenOf<List<Ordered>>(), List::class.java, MediaType.APPLICATION_JSON)).isFalse()

assertThat(converter.canRead(ResolvableType.NONE.type, null, MediaType.APPLICATION_JSON)).isFalse()
}

@Test
Expand All @@ -83,6 +86,8 @@ class KotlinSerializationJsonHttpMessageConverterTests {
assertThat(converter.canWrite(typeTokenOf<List<Int>>(), List::class.java, MediaType.APPLICATION_PDF)).isFalse()

assertThat(converter.canWrite(typeTokenOf<Ordered>(), Ordered::class.java, MediaType.APPLICATION_JSON)).isFalse()

assertThat(converter.canWrite(ResolvableType.NONE.type, SerializableBean::class.java, MediaType.APPLICATION_JSON)).isFalse()
}

@Test
Expand Down

0 comments on commit 42ab1b7

Please sign in to comment.