Use ParameterizedTypeReference in public-facing WebFlux APIs

This commit changes the use of `ResolvableType` to
`ParameterizedTypeReference` in all public-facing WebFlux APIs. This
change removes the necessity for providing the parameterized type
information twice: once for creating the `ResolvableType`, and once for
specifying a `BodyExtractor`.

Issue: SPR-15636
This commit is contained in:
Arjen Poutsma
2017-06-07 17:29:56 +02:00
parent b6c09fa76a
commit 5e954dcba0
10 changed files with 119 additions and 58 deletions

View File

@@ -32,18 +32,20 @@ import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.test.util.JsonExpectationsHelper;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
@@ -289,20 +291,19 @@ class DefaultWebTestClient implements WebTestClient {
this.timeout = timeout;
}
@SuppressWarnings("unchecked")
public <T> EntityExchangeResult<T> decode(ResolvableType bodyType) {
T body = (T) this.response.body(toMono(bodyType)).block(this.timeout);
public <T> EntityExchangeResult<T> decode(BodyExtractor<Mono<T>, ? super ClientHttpResponse> extractor) {
T body = this.response.body(extractor).block(this.timeout);
return new EntityExchangeResult<>(this, body);
}
public <T> EntityExchangeResult<List<T>> decodeToList(ResolvableType elementType) {
Flux<T> flux = this.response.body(toFlux(elementType));
public <T> EntityExchangeResult<List<T>> decodeToList(BodyExtractor<Flux<T>, ? super ClientHttpResponse> extractor) {
Flux<T> flux = this.response.body(extractor);
List<T> body = flux.collectList().block(this.timeout);
return new EntityExchangeResult<>(this, body);
}
public <T> FluxExchangeResult<T> decodeToFlux(ResolvableType elementType) {
Flux<T> body = this.response.body(toFlux(elementType));
public <T> FluxExchangeResult<T> decodeToFlux(BodyExtractor<Flux<T>, ? super ClientHttpResponse> extractor) {
Flux<T> body = this.response.body(extractor);
return new FluxExchangeResult<>(this, body, this.timeout);
}
@@ -333,25 +334,23 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
@SuppressWarnings("unchecked")
public <B> BodySpec<B, ?> expectBody(Class<B> bodyType) {
return (BodySpec<B, ?>) expectBody(ResolvableType.forClass(bodyType));
return new DefaultBodySpec<>(this.result.decode(toMono(bodyType)));
}
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public <B> BodySpec<B, ?> expectBody(ResolvableType bodyType) {
return new DefaultBodySpec(this.result.decode(bodyType));
public <B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType) {
return new DefaultBodySpec<>(this.result.decode(toMono(bodyType)));
}
@Override
public <E> ListBodySpec<E> expectBodyList(Class<E> elementType) {
return expectBodyList(ResolvableType.forClass(elementType));
return new DefaultListBodySpec<>(this.result.decodeToList(toFlux(elementType)));
}
@Override
public <E> ListBodySpec<E> expectBodyList(ResolvableType elementType) {
return new DefaultListBodySpec<>(this.result.decodeToList(elementType));
public <E> ListBodySpec<E> expectBodyList(ParameterizedTypeReference<E> elementType) {
return new DefaultListBodySpec<>(this.result.decodeToList(toFlux(elementType)));
}
@Override
@@ -361,12 +360,12 @@ class DefaultWebTestClient implements WebTestClient {
@Override
public <T> FluxExchangeResult<T> returnResult(Class<T> elementType) {
return returnResult(ResolvableType.forClass(elementType));
return this.result.decodeToFlux(toFlux(elementType));
}
@Override
public <T> FluxExchangeResult<T> returnResult(ResolvableType elementType) {
return this.result.decodeToFlux(elementType);
public <T> FluxExchangeResult<T> returnResult(ParameterizedTypeReference<T> elementType) {
return this.result.decodeToFlux(toFlux(elementType));
}
}

View File

@@ -28,7 +28,7 @@ import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.context.ApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -534,7 +534,7 @@ public interface WebTestClient {
/**
* Variant of {@link #expectBody(Class)} for a body type with generics.
*/
<B> BodySpec<B, ?> expectBody(ResolvableType bodyType);
<B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType);
/**
* Declare expectations on the response body decoded to {@code List<E>}.
@@ -545,7 +545,7 @@ public interface WebTestClient {
/**
* Variant of {@link #expectBodyList(Class)} for element types with generics.
*/
<E> ListBodySpec<E> expectBodyList(ResolvableType elementType);
<E> ListBodySpec<E> expectBodyList(ParameterizedTypeReference<E> elementType);
/**
* Declare expectations on the response body content.
@@ -565,7 +565,7 @@ public interface WebTestClient {
/**
* Variant of {@link #returnResult(Class)} for element types with generics.
*/
<T> FluxExchangeResult<T> returnResult(ResolvableType elementType);
<T> FluxExchangeResult<T> returnResult(ParameterizedTypeReference<T> elementType);
}
/**