Skip to content

Commit

Permalink
Fix RestClient generic type handling
Browse files Browse the repository at this point in the history
For client side use case, the context class should be null,
consistently with what is done in HttpMessageConverterExtractor.

Closes gh-31574
  • Loading branch information
sdeleuze committed Nov 9, 2023
1 parent 55d13a0 commit 38724a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* as created by the static factory methods.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @since 6.1
* @see RestClient#create()
* @see RestClient#create(String)
Expand Down Expand Up @@ -609,11 +610,11 @@ private <T> T readWithMessageConverters(Type bodyType, Class<T> bodyClass) {

for (HttpMessageConverter<?> messageConverter : DefaultRestClient.this.messageConverters) {
if (messageConverter instanceof GenericHttpMessageConverter genericHttpMessageConverter) {
if (genericHttpMessageConverter.canRead(bodyType, bodyClass, contentType)) {
if (genericHttpMessageConverter.canRead(bodyType, null, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Reading to [" + ResolvableType.forType(bodyType) + "]");
}
return (T) genericHttpMessageConverter.read(bodyType, bodyClass, this.clientResponse);
return (T) genericHttpMessageConverter.read(bodyType, null, this.clientResponse);
}
}
if (messageConverter.canRead(bodyClass, contentType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* Integration tests for {@link RestClient}.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
*/
class RestClientIntegrationTests {

Expand Down Expand Up @@ -179,6 +180,29 @@ void retrieveJsonWithParameterizedTypeReference(ClientHttpRequestFactory request
});
}

@ParameterizedRestClientTest
void retrieveJsonWithListParameterizedTypeReference(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);

String content = "{\"containerValue\":[{\"bar\":\"barbar\",\"foo\":\"foofoo\"}]}";
prepareResponse(response -> response
.setHeader("Content-Type", "application/json").setBody(content));

ValueContainer<List<Pojo>> result = this.restClient.get()
.uri("/json").accept(MediaType.APPLICATION_JSON)
.retrieve()
.body(new ParameterizedTypeReference<ValueContainer<List<Pojo>>>() {});

assertThat(result.containerValue).isNotNull();
assertThat(result.containerValue).containsExactly(new Pojo("foofoo", "barbar"));

expectRequestCount(1);
expectRequest(request -> {
assertThat(request.getPath()).isEqualTo("/json");
assertThat(request.getHeader(HttpHeaders.ACCEPT)).isEqualTo("application/json");
});
}

@ParameterizedRestClientTest
void retrieveJsonAsResponseEntity(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);
Expand Down

0 comments on commit 38724a1

Please sign in to comment.