Skip to content

Commit

Permalink
Fix Kotlin Serialization converter registration logic
Browse files Browse the repository at this point in the history
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
  • Loading branch information
meloning authored and sdeleuze committed Sep 2, 2022
1 parent 6a68bd5 commit bb3ada4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
Expand Up @@ -461,6 +461,9 @@ public CompositeMessageConverter brokerMessageConverter() {
if (registerDefaults) {
converters.add(new StringMessageConverter());
converters.add(new ByteArrayMessageConverter());
if (kotlinSerializationJsonPresent) {
converters.add(new KotlinSerializationJsonMessageConverter());
}
if (jackson2Present) {
converters.add(createJacksonConverter());
}
Expand All @@ -470,9 +473,6 @@ else if (gsonPresent) {
else if (jsonbPresent) {
converters.add(new JsonbMessageConverter());
}
else if (kotlinSerializationJsonPresent) {
converters.add(new KotlinSerializationJsonMessageConverter());
}
}
return new CompositeMessageConverter(converters);
}
Expand Down
Expand Up @@ -39,6 +39,7 @@
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;
Expand Down Expand Up @@ -282,12 +283,13 @@ public void configureMessageConvertersDefault() {
CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

List<MessageConverter> 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);
}

Expand Down Expand Up @@ -340,11 +342,12 @@ protected boolean configureMessageConverters(List<MessageConverter> messageConve
};
CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

assertThat(compositeConverter.getConverters()).hasSize(4);
assertThat(compositeConverter.getConverters()).hasSize(5);
Iterator<MessageConverter> 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);
}

Expand Down
Expand Up @@ -87,6 +87,9 @@ public AllEncompassingFormHttpMessageConverter() {
}
}

if (kotlinSerializationJsonPresent) {
addPartConverter(new KotlinSerializationJsonHttpMessageConverter());
}
if (jackson2Present) {
addPartConverter(new MappingJackson2HttpMessageConverter());
}
Expand All @@ -96,9 +99,6 @@ else if (gsonPresent) {
else if (jsonbPresent) {
addPartConverter(new JsonbHttpMessageConverter());
}
else if (kotlinSerializationJsonPresent) {
addPartConverter(new KotlinSerializationJsonHttpMessageConverter());
}

if (jackson2XmlPresent && !shouldIgnoreXml) {
addPartConverter(new MappingJackson2XmlHttpMessageConverter());
Expand Down
Expand Up @@ -174,6 +174,9 @@ else if (jaxb2Present) {
}
}

if (kotlinSerializationJsonPresent) {
this.messageConverters.add(new KotlinSerializationJsonHttpMessageConverter());
}
if (jackson2Present) {
this.messageConverters.add(new MappingJackson2HttpMessageConverter());
}
Expand All @@ -183,9 +186,6 @@ else if (gsonPresent) {
else if (jsonbPresent) {
this.messageConverters.add(new JsonbHttpMessageConverter());
}
else if (kotlinSerializationJsonPresent) {
this.messageConverters.add(new KotlinSerializationJsonHttpMessageConverter());
}

if (jackson2SmilePresent) {
this.messageConverters.add(new MappingJackson2SmileHttpMessageConverter());
Expand Down
Expand Up @@ -51,6 +51,8 @@
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;

Expand Down Expand Up @@ -103,6 +105,16 @@ void setup() {
template.setErrorHandler(errorHandler);
}

@Test // gh-29008
void defaultMessageConvertersWithKotlinSerialization() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> httpMessageConverters = restTemplate.getMessageConverters();
assertThat(httpMessageConverters).extracting("class").containsOnlyOnce(
KotlinSerializationJsonHttpMessageConverter.class,
MappingJackson2HttpMessageConverter.class
);
}

@Test
void constructorPreconditions() {
assertThatIllegalArgumentException()
Expand Down

0 comments on commit bb3ada4

Please sign in to comment.