Refine multipart converter registration in HttpMessageConverters

As of #33894, `HttpMessageConverters` auto-detects converters and use
custom-provided ones to configure a collection of converters for the
client or the server.
Right now the multipart converter is only configured if core converters
(JSON, XML...) are configured/detected. We do not reuse the base
converters (resource, string, byte array) for the multipart converter
as it applies different encoding defaults (ISO for the main ones, UTF-8
for multipart).

This commit refines the configuration to not only include the multipart
converter when core converters are present, but also if any other
converter was configured.

Closes gh-35203
This commit is contained in:
Brian Clozel
2025-07-15 14:45:28 +02:00
parent 2e0cc63616
commit ad007caebc
3 changed files with 34 additions and 11 deletions
@@ -160,6 +160,13 @@ class DefaultHttpMessageConvertersTests {
assertThat(multipartConverter.getPartConverters()).hasAtLeastOneElementOfType(CustomHttpMessageConverter.class);
}
@Test
void registerMultipartConverterWhenOtherConvertersPresent() {
var converters = HttpMessageConverters.forClient()
.stringMessageConverter(new StringHttpMessageConverter()).build();
assertThat(converters).hasExactlyElementsOfTypes(StringHttpMessageConverter.class, AllEncompassingFormHttpMessageConverter.class);
}
@Test
void shouldUseSpecificConverter() {
var jacksonConverter = new JacksonJsonHttpMessageConverter();
@@ -248,7 +255,14 @@ class DefaultHttpMessageConvertersTests {
}
@Test
void shouldUseServerSpecificConverter() {
void registerMultipartConverterWhenOtherConvertersPresent() {
var converters = HttpMessageConverters.forServer()
.stringMessageConverter(new StringHttpMessageConverter()).build();
assertThat(converters).hasExactlyElementsOfTypes(StringHttpMessageConverter.class, AllEncompassingFormHttpMessageConverter.class);
}
@Test
void shouldUseSpecificConverter() {
var jacksonConverter = new JacksonJsonHttpMessageConverter();
var converters = HttpMessageConverters.forServer().registerDefaults()
.jsonMessageConverter(jacksonConverter).build();
@@ -33,6 +33,7 @@ import org.springframework.http.client.JettyClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.web.util.DefaultUriBuilderFactory;
import static org.assertj.core.api.Assertions.assertThat;
@@ -150,7 +151,7 @@ public class RestClientBuilderTests {
assertThat(fieldValue("messageConverters", defaultBuilder))
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsExactly(stringConverter);
.hasExactlyElementsOfTypes(StringHttpMessageConverter.class, AllEncompassingFormHttpMessageConverter.class);
}
@Test