Skip to content

Commit

Permalink
Add RestClient.Builder#messageConverters(List)
Browse files Browse the repository at this point in the history
This commit also introduces the validation of the RestClient
message converters for both variants.

Closes spring-projectsgh-33536
  • Loading branch information
sdeleuze committed Sep 13, 2024
1 parent 0a00148 commit d3755ab
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -63,6 +64,7 @@
*
* @author Arjen Poutsma
* @author Hyoungjune Kim
* @author Sebastien Deleuze
* @since 6.1
*/
final class DefaultRestClientBuilder implements RestClient.Builder {
Expand Down Expand Up @@ -358,6 +360,14 @@ public RestClient.Builder requestFactory(ClientHttpRequestFactory requestFactory
@Override
public RestClient.Builder messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer) {
configurer.accept(initMessageConverters());
validateConverters(this.messageConverters);
return this;
}

@Override
public RestClient.Builder messageConverters(List<HttpMessageConverter<?>> messageConverters) {
validateConverters(messageConverters);
this.messageConverters = Collections.unmodifiableList(messageConverters);
return this;
}

Expand Down Expand Up @@ -413,6 +423,11 @@ else if (jsonbPresent) {
return this.messageConverters;
}

private void validateConverters(@Nullable List<HttpMessageConverter<?>> messageConverters) {
Assert.notEmpty(messageConverters, "At least one HttpMessageConverter is required");
Assert.noNullElements(messageConverters, "The HttpMessageConverter list must not contain null elements");
}


@Override
public RestClient.Builder clone() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
* </ul>
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @since 6.1
*/
public interface RestClient {
Expand Down Expand Up @@ -399,11 +400,22 @@ Builder defaultStatusHandler(Predicate<HttpStatusCode> statusPredicate,

/**
* Configure the message converters for the {@code RestClient} to use.
* @param configurer the configurer to apply
* @param configurer the configurer to apply on the list of default
* {@link HttpMessageConverter} pre-initialized
* @return this builder
* @see #messageConverters(List)
*/
Builder messageConverters(Consumer<List<HttpMessageConverter<?>>> configurer);

/**
* Set the message converters for the {@code RestClient} to use.
* @param messageConverters the list of {@link HttpMessageConverter} to use
* @return this builder
* @since 6.2
* @see #messageConverters(Consumer)
*/
Builder messageConverters(List<HttpMessageConverter<?>> messageConverters);

/**
* Configure the {@link io.micrometer.observation.ObservationRegistry} to use
* for recording HTTP client observations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import java.lang.reflect.Field;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

import org.springframework.http.client.ClientHttpRequestInitializer;
Expand All @@ -32,11 +35,13 @@
import org.springframework.web.util.DefaultUriBuilderFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.fail;


/**
* @author Arjen Poutsma
* @author Sebastien Deleuze
*/
public class RestClientBuilderTests {

Expand Down Expand Up @@ -103,6 +108,35 @@ void defaultUri() {
assertThat(fieldValue("baseUrl", defaultBuilder)).isEqualTo(baseUrl.toString());
}

@Test
void messageConvertersList() {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
RestClient.Builder builder = RestClient.builder();
builder.messageConverters(List.of(stringConverter));

assertThat(builder).isInstanceOf(DefaultRestClientBuilder.class);
DefaultRestClientBuilder defaultBuilder = (DefaultRestClientBuilder) builder;

assertThat(fieldValue("messageConverters", defaultBuilder))
.asInstanceOf(InstanceOfAssertFactories.LIST)
.containsExactly(stringConverter);
}

@Test
void messageConvertersListEmpty() {
RestClient.Builder builder = RestClient.builder();
List<HttpMessageConverter<?>> converters = Collections.emptyList();
assertThatIllegalArgumentException().isThrownBy(() -> builder.messageConverters(converters));
}

@Test
void messageConvertersListWithNullElement() {
RestClient.Builder builder = RestClient.builder();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(null);
assertThatIllegalArgumentException().isThrownBy(() -> builder.messageConverters(converters));
}

@Nullable
private static Object fieldValue(String name, DefaultRestClientBuilder instance) {
try {
Expand Down

0 comments on commit d3755ab

Please sign in to comment.