Skip to content

Commit a5339d7

Browse files
committed
WebClient handles no Content-Type with data correctly
Issue: SPR-17482
1 parent e4c84ec commit a5339d7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyExtractors.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,15 @@ private static <T> Flux<T> unsupportedErrorHandler(
224224

225225
Flux<T> result;
226226
if (message.getHeaders().getContentType() == null) {
227-
// Maybe it's okay, if there is no content..
228-
result = message.getBody().map(o -> {
227+
// Maybe it's okay there is no content type, if there is no content..
228+
result = message.getBody().map(buffer -> {
229+
DataBufferUtils.release(buffer);
229230
throw ex;
230231
});
231232
}
232233
else {
233-
result = Flux.error(ex);
234-
}
235-
if (message instanceof ClientHttpResponse) {
236-
result = consumeAndCancel(message).thenMany(result);
234+
result = message instanceof ClientHttpResponse ?
235+
consumeAndCancel(message).thenMany(Flux.error(ex)) : Flux.error(ex);
237236
}
238237
return result;
239238
}

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.web.reactive.function.client;
1717

1818
import java.time.Duration;
19+
import java.util.Map;
1920
import java.util.function.Function;
2021

2122
import io.netty.buffer.ByteBufAllocator;
@@ -28,12 +29,14 @@
2829
import reactor.core.publisher.Mono;
2930
import reactor.test.StepVerifier;
3031

32+
import org.springframework.core.ParameterizedTypeReference;
3133
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
3234
import org.springframework.core.io.buffer.NettyDataBufferFactory;
3335
import org.springframework.http.HttpStatus;
3436
import org.springframework.http.MediaType;
3537
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
3638
import org.springframework.http.client.reactive.ReactorResourceFactory;
39+
import org.springframework.web.reactive.function.UnsupportedMediaTypeException;
3740

3841
import static org.junit.Assert.*;
3942

@@ -103,6 +106,21 @@ public void bodyToMonoVoid() {
103106
assertEquals(1, this.server.getRequestCount());
104107
}
105108

109+
@Test // SPR-17482
110+
public void bodyToMonoVoidWithoutContentType() {
111+
112+
this.server.enqueue(new MockResponse()
113+
.setResponseCode(HttpStatus.ACCEPTED.value())
114+
.setChunkedBody("{\"foo\" : \"123\", \"baz\" : \"456\", \"baz\" : \"456\"}", 5));
115+
116+
Mono<Map<String, String>> mono = this.webClient.get()
117+
.uri("/sample").accept(MediaType.APPLICATION_JSON)
118+
.retrieve()
119+
.bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {});
120+
121+
StepVerifier.create(mono).expectError(UnsupportedMediaTypeException.class).verify(Duration.ofSeconds(3));
122+
assertEquals(1, this.server.getRequestCount());
123+
}
106124

107125
@Test
108126
public void onStatusWithBodyNotConsumed() {

0 commit comments

Comments
 (0)