Improve support for Mono<ResponseEntity<?>>
If the body class is not resolvable from the return type and there is a body instance we now fall back on the class of the body instance. Issue: SPR-14877
This commit is contained in:
@@ -71,7 +71,7 @@ public class HandlerResultHandlerTests {
|
||||
public void usesContentTypeResolver() throws Exception {
|
||||
TestResultHandler resultHandler = new TestResultHandler(new FixedContentTypeResolver(IMAGE_GIF));
|
||||
List<MediaType> mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG);
|
||||
MediaType actual = resultHandler.selectMediaType(this.exchange, mediaTypes);
|
||||
MediaType actual = resultHandler.selectMediaType(this.exchange, () -> mediaTypes);
|
||||
|
||||
assertEquals(IMAGE_GIF, actual);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public class HandlerResultHandlerTests {
|
||||
this.exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, producible);
|
||||
|
||||
List<MediaType> mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG);
|
||||
MediaType actual = resultHandler.selectMediaType(this.exchange, mediaTypes);
|
||||
MediaType actual = resultHandler.selectMediaType(this.exchange, () -> mediaTypes);
|
||||
|
||||
assertEquals(IMAGE_GIF, actual);
|
||||
}
|
||||
@@ -92,7 +92,7 @@ public class HandlerResultHandlerTests {
|
||||
this.request.setHeader("Accept", "text/plain; q=0.5, application/json");
|
||||
|
||||
List<MediaType> mediaTypes = Arrays.asList(TEXT_PLAIN, APPLICATION_JSON_UTF8);
|
||||
MediaType actual = this.resultHandler.selectMediaType(this.exchange, mediaTypes);
|
||||
MediaType actual = this.resultHandler.selectMediaType(this.exchange, () -> mediaTypes);
|
||||
|
||||
assertEquals(APPLICATION_JSON_UTF8, actual);
|
||||
}
|
||||
@@ -102,7 +102,8 @@ public class HandlerResultHandlerTests {
|
||||
MediaType text8859 = MediaType.parseMediaType("text/plain;charset=ISO-8859-1");
|
||||
MediaType textUtf8 = MediaType.parseMediaType("text/plain;charset=UTF-8");
|
||||
this.request.getHeaders().setAccept(Collections.singletonList(text8859));
|
||||
MediaType actual = this.resultHandler.selectMediaType(this.exchange, Collections.singletonList(textUtf8));
|
||||
MediaType actual = this.resultHandler.selectMediaType(this.exchange,
|
||||
() -> Collections.singletonList(textUtf8));
|
||||
|
||||
assertEquals(text8859, actual);
|
||||
}
|
||||
@@ -110,7 +111,7 @@ public class HandlerResultHandlerTests {
|
||||
@Test // SPR-12894
|
||||
public void noConcreteMediaType() throws Exception {
|
||||
List<MediaType> producible = Collections.singletonList(ALL);
|
||||
MediaType actual = this.resultHandler.selectMediaType(this.exchange, producible);
|
||||
MediaType actual = this.resultHandler.selectMediaType(this.exchange, () -> producible);
|
||||
|
||||
assertEquals(APPLICATION_OCTET_STREAM, actual);
|
||||
}
|
||||
|
||||
@@ -43,7 +43,6 @@ import org.springframework.core.codec.ByteBufferEncoder;
|
||||
import org.springframework.core.codec.CharSequenceEncoder;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||
@@ -51,9 +50,9 @@ import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.ResourceHttpMessageWriter;
|
||||
import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
||||
import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||
@@ -211,7 +210,7 @@ public class MessageWriterResultHandlerTests {
|
||||
public ParentClass() {
|
||||
}
|
||||
|
||||
public ParentClass(String parentProperty) {
|
||||
ParentClass(String parentProperty) {
|
||||
this.parentProperty = parentProperty;
|
||||
}
|
||||
|
||||
@@ -235,7 +234,7 @@ public class MessageWriterResultHandlerTests {
|
||||
@JsonTypeName("bar")
|
||||
private static class Bar extends ParentClass {
|
||||
|
||||
public Bar(String parentProperty) {
|
||||
Bar(String parentProperty) {
|
||||
super(parentProperty);
|
||||
}
|
||||
}
|
||||
@@ -253,7 +252,7 @@ public class MessageWriterResultHandlerTests {
|
||||
|
||||
private String name;
|
||||
|
||||
public SimpleBean(Long id, String name) {
|
||||
SimpleBean(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -37,11 +38,11 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.ByteBufferEncoder;
|
||||
import org.springframework.core.codec.CharSequenceEncoder;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
@@ -51,6 +52,7 @@ import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.HandlerResult;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
|
||||
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
|
||||
@@ -270,6 +272,23 @@ public class ResponseEntityResultHandlerTests {
|
||||
assertConditionalResponse(HttpStatus.OK, "body", newEtag, oneMinAgo);
|
||||
}
|
||||
|
||||
@Test // SPR-14877
|
||||
public void handleMonoWithWildcardBodyType() throws Exception {
|
||||
|
||||
this.exchange.getAttributes().put(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE,
|
||||
Collections.singleton(MediaType.APPLICATION_JSON));
|
||||
|
||||
HandlerResult result = new HandlerResult(new TestController(), Mono.just(ok().body("body")),
|
||||
ResolvableMethod.onClass(TestController.class)
|
||||
.name("monoResponseEntityWildcard")
|
||||
.resolveReturnType());
|
||||
|
||||
this.resultHandler.handleResult(this.exchange, result).block(Duration.ofSeconds(5));
|
||||
|
||||
assertEquals(HttpStatus.OK, this.response.getStatusCode());
|
||||
assertResponseBody("\"body\"");
|
||||
}
|
||||
|
||||
|
||||
private void testHandle(Object returnValue, ResolvableType type) {
|
||||
HandlerResult result = handlerResult(returnValue, type);
|
||||
@@ -333,6 +352,9 @@ public class ResponseEntityResultHandlerTests {
|
||||
String string() { return null; }
|
||||
|
||||
Completable completable() { return null; }
|
||||
|
||||
Mono<ResponseEntity<?>> monoResponseEntityWildcard() { return null; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user