Skip to content

Commit 46c0366

Browse files
committed
Merge branch '5.1.x'
2 parents 81ec1d8 + d6aa589 commit 46c0366

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import reactor.core.publisher.Mono;
3434

3535
import org.springframework.core.ParameterizedTypeReference;
36+
import org.springframework.core.codec.DecodingException;
3637
import org.springframework.core.codec.Hints;
3738
import org.springframework.http.HttpCookie;
3839
import org.springframework.http.HttpHeaders;
@@ -48,6 +49,7 @@
4849
import org.springframework.web.reactive.function.BodyExtractors;
4950
import org.springframework.web.reactive.function.UnsupportedMediaTypeException;
5051
import org.springframework.web.server.ServerWebExchange;
52+
import org.springframework.web.server.ServerWebInputException;
5153
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
5254
import org.springframework.web.server.WebSession;
5355
import org.springframework.web.util.UriBuilder;
@@ -67,6 +69,9 @@ class DefaultServerRequest implements ServerRequest {
6769
ex.getContentType(), ex.getSupportedMediaTypes(), ex.getBodyType()) :
6870
new UnsupportedMediaTypeStatusException(ex.getMessage()));
6971

72+
private static final Function<DecodingException, ServerWebInputException> DECODING_MAPPER =
73+
ex -> new ServerWebInputException("Failed to read HTTP message", null, ex);
74+
7075

7176
private final ServerWebExchange exchange;
7277

@@ -154,25 +159,29 @@ public Map<String, Object> hints() {
154159
@Override
155160
public <T> Mono<T> bodyToMono(Class<? extends T> elementClass) {
156161
Mono<T> mono = body(BodyExtractors.toMono(elementClass));
157-
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
162+
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
163+
.onErrorMap(DecodingException.class, DECODING_MAPPER);
158164
}
159165

160166
@Override
161167
public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference) {
162168
Mono<T> mono = body(BodyExtractors.toMono(typeReference));
163-
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
169+
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
170+
.onErrorMap(DecodingException.class, DECODING_MAPPER);
164171
}
165172

166173
@Override
167174
public <T> Flux<T> bodyToFlux(Class<? extends T> elementClass) {
168175
Flux<T> flux = body(BodyExtractors.toFlux(elementClass));
169-
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
176+
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
177+
.onErrorMap(DecodingException.class, DECODING_MAPPER);
170178
}
171179

172180
@Override
173181
public <T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> typeReference) {
174182
Flux<T> flux = body(BodyExtractors.toFlux(typeReference));
175-
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
183+
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
184+
.onErrorMap(DecodingException.class, DECODING_MAPPER);
176185
}
177186

178187
@Override

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.nio.ByteBuffer;
2323
import java.nio.charset.Charset;
2424
import java.nio.charset.StandardCharsets;
25+
import java.util.Arrays;
2526
import java.util.Collections;
2627
import java.util.List;
2728
import java.util.Map;
@@ -45,12 +46,14 @@
4546
import org.springframework.http.MediaType;
4647
import org.springframework.http.codec.DecoderHttpMessageReader;
4748
import org.springframework.http.codec.HttpMessageReader;
49+
import org.springframework.http.codec.json.Jackson2JsonDecoder;
4850
import org.springframework.http.codec.multipart.FormFieldPart;
4951
import org.springframework.http.codec.multipart.Part;
5052
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
5153
import org.springframework.mock.web.test.server.MockServerWebExchange;
5254
import org.springframework.util.LinkedMultiValueMap;
5355
import org.springframework.util.MultiValueMap;
56+
import org.springframework.web.server.ServerWebInputException;
5457
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
5558

5659
import static org.junit.Assert.*;
@@ -61,7 +64,8 @@
6164
*/
6265
public class DefaultServerRequestTests {
6366

64-
private final List<HttpMessageReader<?>> messageReaders = Collections.singletonList(
67+
private final List<HttpMessageReader<?>> messageReaders = Arrays.asList(
68+
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()),
6569
new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
6670

6771

@@ -279,6 +283,28 @@ public void bodyToMonoParameterizedTypeReference() {
279283
assertEquals("foo", resultMono.block());
280284
}
281285

286+
@Test
287+
public void bodyToMonoDecodingException() {
288+
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
289+
DefaultDataBuffer dataBuffer =
290+
factory.wrap(ByteBuffer.wrap("{\"invalid\":\"json\" ".getBytes(StandardCharsets.UTF_8)));
291+
Flux<DataBuffer> body = Flux.just(dataBuffer);
292+
293+
HttpHeaders httpHeaders = new HttpHeaders();
294+
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
295+
MockServerHttpRequest mockRequest = MockServerHttpRequest
296+
.method(HttpMethod.POST, "http://example.com/invalid")
297+
.headers(httpHeaders)
298+
.body(body);
299+
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);
300+
301+
Mono<Map<String, String>> resultMono = request.bodyToMono(
302+
new ParameterizedTypeReference<Map<String, String>>() {});
303+
StepVerifier.create(resultMono)
304+
.expectError(ServerWebInputException.class)
305+
.verify();
306+
}
307+
282308
@Test
283309
public void bodyToFlux() {
284310
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();

0 commit comments

Comments
 (0)