Skip to content

Commit

Permalink
WebClientResponseException supports decoding empty content
Browse files Browse the repository at this point in the history
Closes gh-31179
  • Loading branch information
rstoyanchev committed Sep 7, 2023
1 parent 249f6f2 commit 7a51617
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,6 +50,7 @@
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyExtractors;

Expand Down Expand Up @@ -233,6 +234,9 @@ public Mono<WebClientResponseException> createException() {

private Function<ResolvableType, ?> initDecodeFunction(byte[] body, @Nullable MediaType contentType) {
return targetType -> {
if (ObjectUtils.isEmpty(body)) {
return null;
}
Decoder<?> decoder = null;
for (HttpMessageReader<?> reader : strategies().messageReaders()) {
if (reader.canRead(targetType, contentType)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -233,22 +233,21 @@ public String getResponseBodyAsString(Charset defaultCharset) {
*/
@Nullable
public <E> E getResponseBodyAs(Class<E> targetType) {
return getResponseBodyAs(ResolvableType.forClass(targetType));
return decodeBody(ResolvableType.forClass(targetType));
}

/**
* Variant of {@link #getResponseBodyAs(Class)} with
* {@link ParameterizedTypeReference}.
* Variant of {@link #getResponseBodyAs(Class)} with {@link ParameterizedTypeReference}.
* @since 6.0
*/
@Nullable
public <E> E getResponseBodyAs(ParameterizedTypeReference<E> targetType) {
return getResponseBodyAs(ResolvableType.forType(targetType.getType()));
return decodeBody(ResolvableType.forType(targetType.getType()));
}

@SuppressWarnings("unchecked")
@Nullable
private <E> E getResponseBodyAs(ResolvableType targetType) {
private <E> E decodeBody(ResolvableType targetType) {
Assert.state(this.bodyDecodeFunction != null, "Decoder function not set");
return (E) this.bodyDecodeFunction.apply(targetType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -45,6 +46,7 @@
import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

Expand Down Expand Up @@ -346,6 +348,40 @@ void createException() {
assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
}

@Test
void createExceptionAndDecodeContent() {
byte[] bytes = "{\"name\":\"Jason\"}".getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);

httpHeaders.setContentType(MediaType.APPLICATION_JSON);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(Flux.just(buffer));

given(mockExchangeStrategies.messageReaders()).willReturn(List.of(
new DecoderHttpMessageReader<>(new ByteArrayDecoder()),
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));

WebClientResponseException ex = defaultClientResponse.createException().block();
assertThat(ex.getResponseBodyAs(Map.class)).hasSize(1).containsEntry("name", "Jason");
}

@Test
void createExceptionAndDecodeWithoutContent() {
byte[] bytes = new byte[0];
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);

httpHeaders.setContentType(MediaType.APPLICATION_JSON);
given(mockResponse.getStatusCode()).willReturn(HttpStatus.NOT_FOUND);
given(mockResponse.getBody()).willReturn(Flux.just(buffer));

given(mockExchangeStrategies.messageReaders()).willReturn(List.of(
new DecoderHttpMessageReader<>(new ByteArrayDecoder()),
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));

WebClientResponseException ex = defaultClientResponse.createException().block();
assertThat(ex.getResponseBodyAs(Map.class)).isNull();
}

@Test
@SuppressWarnings("deprecation")
void createError() {
Expand Down

0 comments on commit 7a51617

Please sign in to comment.