Browse Source

Merge branch '5.1.x'

pull/22297/head
Brian Clozel 7 years ago
parent
commit
46c0366594
  1. 19
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java
  2. 30
      spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerRequestTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@ -33,6 +33,7 @@ import reactor.core.publisher.Flux; @@ -33,6 +33,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.codec.Hints;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
@ -48,6 +49,7 @@ import org.springframework.web.reactive.function.BodyExtractor; @@ -48,6 +49,7 @@ import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.reactive.function.UnsupportedMediaTypeException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import org.springframework.web.server.WebSession;
import org.springframework.web.util.UriBuilder;
@ -67,6 +69,9 @@ class DefaultServerRequest implements ServerRequest { @@ -67,6 +69,9 @@ class DefaultServerRequest implements ServerRequest {
ex.getContentType(), ex.getSupportedMediaTypes(), ex.getBodyType()) :
new UnsupportedMediaTypeStatusException(ex.getMessage()));
private static final Function<DecodingException, ServerWebInputException> DECODING_MAPPER =
ex -> new ServerWebInputException("Failed to read HTTP message", null, ex);
private final ServerWebExchange exchange;
@ -154,25 +159,29 @@ class DefaultServerRequest implements ServerRequest { @@ -154,25 +159,29 @@ class DefaultServerRequest implements ServerRequest {
@Override
public <T> Mono<T> bodyToMono(Class<? extends T> elementClass) {
Mono<T> mono = body(BodyExtractors.toMono(elementClass));
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
.onErrorMap(DecodingException.class, DECODING_MAPPER);
}
@Override
public <T> Mono<T> bodyToMono(ParameterizedTypeReference<T> typeReference) {
Mono<T> mono = body(BodyExtractors.toMono(typeReference));
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
return mono.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
.onErrorMap(DecodingException.class, DECODING_MAPPER);
}
@Override
public <T> Flux<T> bodyToFlux(Class<? extends T> elementClass) {
Flux<T> flux = body(BodyExtractors.toFlux(elementClass));
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
.onErrorMap(DecodingException.class, DECODING_MAPPER);
}
@Override
public <T> Flux<T> bodyToFlux(ParameterizedTypeReference<T> typeReference) {
Flux<T> flux = body(BodyExtractors.toFlux(typeReference));
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER);
return flux.onErrorMap(UnsupportedMediaTypeException.class, ERROR_MAPPER)
.onErrorMap(DecodingException.class, DECODING_MAPPER);
}
@Override

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@ -22,6 +22,7 @@ import java.net.URISyntaxException; @@ -22,6 +22,7 @@ import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -45,12 +46,14 @@ import org.springframework.http.HttpRange; @@ -45,12 +46,14 @@ import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.codec.DecoderHttpMessageReader;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.multipart.FormFieldPart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import static org.junit.Assert.*;
@ -61,7 +64,8 @@ import static org.springframework.web.reactive.function.BodyExtractors.*; @@ -61,7 +64,8 @@ import static org.springframework.web.reactive.function.BodyExtractors.*;
*/
public class DefaultServerRequestTests {
private final List<HttpMessageReader<?>> messageReaders = Collections.singletonList(
private final List<HttpMessageReader<?>> messageReaders = Arrays.asList(
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()),
new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
@ -279,6 +283,28 @@ public class DefaultServerRequestTests { @@ -279,6 +283,28 @@ public class DefaultServerRequestTests {
assertEquals("foo", resultMono.block());
}
@Test
public void bodyToMonoDecodingException() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer =
factory.wrap(ByteBuffer.wrap("{\"invalid\":\"json\" ".getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
MockServerHttpRequest mockRequest = MockServerHttpRequest
.method(HttpMethod.POST, "http://example.com/invalid")
.headers(httpHeaders)
.body(body);
DefaultServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), messageReaders);
Mono<Map<String, String>> resultMono = request.bodyToMono(
new ParameterizedTypeReference<Map<String, String>>() {});
StepVerifier.create(resultMono)
.expectError(ServerWebInputException.class)
.verify();
}
@Test
public void bodyToFlux() {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();

Loading…
Cancel
Save