From bb3ada4a673c689b9d5c854ec021b8e96e32c447 Mon Sep 17 00:00:00 2001 From: junsu Date: Wed, 24 Aug 2022 23:19:45 +0900 Subject: [PATCH] Fix Kotlin Serialization converter registration logic This commit fixes Kotlin Serialization converter registration logic in RestTemplate, AbstractMessageBrokerConfiguration and AllEncompassingFormHttpMessageConverter classes to be similar to the one in WebMvcConfigurationSupport. Closes gh-29008 --- .../config/AbstractMessageBrokerConfiguration.java | 6 +++--- .../simp/config/MessageBrokerConfigurationTests.java | 11 +++++++---- .../AllEncompassingFormHttpMessageConverter.java | 6 +++--- .../org/springframework/web/client/RestTemplate.java | 6 +++--- .../web/client/RestTemplateTests.java | 12 ++++++++++++ 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java index fa7ef11fe0..3583a15193 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java @@ -461,6 +461,9 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC if (registerDefaults) { converters.add(new StringMessageConverter()); converters.add(new ByteArrayMessageConverter()); + if (kotlinSerializationJsonPresent) { + converters.add(new KotlinSerializationJsonMessageConverter()); + } if (jackson2Present) { converters.add(createJacksonConverter()); } @@ -470,9 +473,6 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC else if (jsonbPresent) { converters.add(new JsonbMessageConverter()); } - else if (kotlinSerializationJsonPresent) { - converters.add(new KotlinSerializationJsonMessageConverter()); - } } return new CompositeMessageConverter(converters); } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java index b31e95942b..3110f6b9ee 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java @@ -39,6 +39,7 @@ import org.springframework.messaging.converter.ByteArrayMessageConverter; import org.springframework.messaging.converter.CompositeMessageConverter; import org.springframework.messaging.converter.ContentTypeResolver; import org.springframework.messaging.converter.DefaultContentTypeResolver; +import org.springframework.messaging.converter.KotlinSerializationJsonMessageConverter; import org.springframework.messaging.converter.MappingJackson2MessageConverter; import org.springframework.messaging.converter.MessageConverter; import org.springframework.messaging.converter.StringMessageConverter; @@ -282,12 +283,13 @@ public class MessageBrokerConfigurationTests { CompositeMessageConverter compositeConverter = config.brokerMessageConverter(); List converters = compositeConverter.getConverters(); - assertThat(converters).hasSize(3); + assertThat(converters).hasSize(4); assertThat(converters.get(0)).isInstanceOf(StringMessageConverter.class); assertThat(converters.get(1)).isInstanceOf(ByteArrayMessageConverter.class); - assertThat(converters.get(2)).isInstanceOf(MappingJackson2MessageConverter.class); + assertThat(converters.get(2)).isInstanceOf(KotlinSerializationJsonMessageConverter.class); + assertThat(converters.get(3)).isInstanceOf(MappingJackson2MessageConverter.class); - ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(2)).getContentTypeResolver(); + ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(3)).getContentTypeResolver(); assertThat(((DefaultContentTypeResolver) resolver).getDefaultMimeType()).isEqualTo(MimeTypeUtils.APPLICATION_JSON); } @@ -340,11 +342,12 @@ public class MessageBrokerConfigurationTests { }; CompositeMessageConverter compositeConverter = config.brokerMessageConverter(); - assertThat(compositeConverter.getConverters()).hasSize(4); + assertThat(compositeConverter.getConverters()).hasSize(5); Iterator iterator = compositeConverter.getConverters().iterator(); assertThat(iterator.next()).isEqualTo(testConverter); assertThat(iterator.next()).isInstanceOf(StringMessageConverter.class); assertThat(iterator.next()).isInstanceOf(ByteArrayMessageConverter.class); + assertThat(iterator.next()).isInstanceOf(KotlinSerializationJsonMessageConverter.class); assertThat(iterator.next()).isInstanceOf(MappingJackson2MessageConverter.class); } diff --git a/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java index 78d64e5da4..f75f85400f 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/support/AllEncompassingFormHttpMessageConverter.java @@ -87,6 +87,9 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv } } + if (kotlinSerializationJsonPresent) { + addPartConverter(new KotlinSerializationJsonHttpMessageConverter()); + } if (jackson2Present) { addPartConverter(new MappingJackson2HttpMessageConverter()); } @@ -96,9 +99,6 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv else if (jsonbPresent) { addPartConverter(new JsonbHttpMessageConverter()); } - else if (kotlinSerializationJsonPresent) { - addPartConverter(new KotlinSerializationJsonHttpMessageConverter()); - } if (jackson2XmlPresent && !shouldIgnoreXml) { addPartConverter(new MappingJackson2XmlHttpMessageConverter()); diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java index 2266263247..98902d6f01 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java @@ -174,6 +174,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat } } + if (kotlinSerializationJsonPresent) { + this.messageConverters.add(new KotlinSerializationJsonHttpMessageConverter()); + } if (jackson2Present) { this.messageConverters.add(new MappingJackson2HttpMessageConverter()); } @@ -183,9 +186,6 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat else if (jsonbPresent) { this.messageConverters.add(new JsonbHttpMessageConverter()); } - else if (kotlinSerializationJsonPresent) { - this.messageConverters.add(new KotlinSerializationJsonHttpMessageConverter()); - } if (jackson2SmilePresent) { this.messageConverters.add(new MappingJackson2SmileHttpMessageConverter()); diff --git a/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java b/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java index c95c60cb28..3df1ce1f28 100644 --- a/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java @@ -51,6 +51,8 @@ import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.GenericHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.StreamUtils; import org.springframework.web.util.DefaultUriBuilderFactory; @@ -103,6 +105,16 @@ class RestTemplateTests { template.setErrorHandler(errorHandler); } + @Test // gh-29008 + void defaultMessageConvertersWithKotlinSerialization() { + RestTemplate restTemplate = new RestTemplate(); + List> httpMessageConverters = restTemplate.getMessageConverters(); + assertThat(httpMessageConverters).extracting("class").containsOnlyOnce( + KotlinSerializationJsonHttpMessageConverter.class, + MappingJackson2HttpMessageConverter.class + ); + } + @Test void constructorPreconditions() { assertThatIllegalArgumentException()