Skip to content

Commit 37f9ce5

Browse files
committed
Return 500 (not 406) if content-type was preset
If content-type is preset in the returned ResponseEntity, then any failure to find a matching converter is a server error. Closes gh-23205
1 parent 3d913b8 commit 37f9ce5

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,17 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
161161
}
162162
}
163163

164+
MediaType contentType = exchange.getResponse().getHeaders().getContentType();
165+
if (contentType != null && contentType.equals(bestMediaType)) {
166+
return Mono.error(new IllegalStateException(
167+
"No Encoder for [" + elementType + "] with preset Content-Type '" + contentType + "'"));
168+
}
169+
164170
List<MediaType> mediaTypes = getMediaTypesFor(elementType);
165171
if (bestMediaType == null && mediaTypes.isEmpty()) {
166172
return Mono.error(new IllegalStateException("No HttpMessageWriter for " + elementType));
167173
}
174+
168175
return Mono.error(new NotAcceptableStatusException(mediaTypes));
169176
}
170177

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void handleMonoWithWildcardBodyTypeAndNullBody() throws Exception {
337337
}
338338

339339
@Test // SPR-17082
340-
public void handleResponseEntityWithExistingResponseHeaders() throws Exception {
340+
public void handleWithPresetContentType() {
341341
ResponseEntity<Void> value = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).build();
342342
MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
343343
HandlerResult result = handlerResult(value, returnType);
@@ -351,6 +351,24 @@ public void handleResponseEntityWithExistingResponseHeaders() throws Exception {
351351
assertResponseBodyIsEmpty(exchange);
352352
}
353353

354+
@Test // gh-23205
355+
public void handleWithPresetContentTypeShouldFailWithServerError() {
356+
ResponseEntity<String> value = ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body("<foo/>");
357+
MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
358+
HandlerResult result = handlerResult(value, returnType);
359+
360+
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
361+
ResponseEntityResultHandler resultHandler = new ResponseEntityResultHandler(
362+
Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly())),
363+
new RequestedContentTypeResolverBuilder().build()
364+
);
365+
366+
StepVerifier.create(resultHandler.handleResult(exchange, result))
367+
.consumeErrorWith(ex -> assertThat(ex)
368+
.isInstanceOf(IllegalStateException.class)
369+
.hasMessageContaining("with preset Content-Type"))
370+
.verify();
371+
}
354372

355373

356374
private void testHandle(Object returnValue, MethodParameter returnType) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter
218218

219219
MediaType selectedMediaType = null;
220220
MediaType contentType = outputMessage.getHeaders().getContentType();
221-
if (contentType != null && contentType.isConcrete()) {
221+
boolean isContentTypePreset = contentType != null && contentType.isConcrete();
222+
if (isContentTypePreset) {
222223
if (logger.isDebugEnabled()) {
223224
logger.debug("Found 'Content-Type:" + contentType + "' in response");
224225
}
@@ -304,6 +305,10 @@ else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
304305
}
305306

306307
if (body != null) {
308+
if (isContentTypePreset) {
309+
throw new IllegalStateException(
310+
"No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
311+
}
307312
throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
308313
}
309314
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
6060
import static org.assertj.core.api.Assertions.assertThat;
6161
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
62+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
6263
import static org.mockito.ArgumentMatchers.any;
6364
import static org.mockito.ArgumentMatchers.anyCollection;
6465
import static org.mockito.ArgumentMatchers.argThat;
@@ -315,6 +316,21 @@ public void shouldFailHandlingWhenContentTypeNotSupported() throws Exception {
315316
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest));
316317
}
317318

319+
@Test // gh-23205
320+
public void shouldFailWithServerErrorIfContentTypeFromResponseEntity() {
321+
ResponseEntity<String> returnValue = ResponseEntity.ok()
322+
.contentType(MediaType.APPLICATION_XML)
323+
.body("<foo/>");
324+
325+
given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
326+
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
327+
328+
assertThatIllegalStateException()
329+
.isThrownBy(() ->
330+
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest))
331+
.withMessageContaining("with preset Content-Type");
332+
}
333+
318334
@Test
319335
public void shouldFailHandlingWhenConverterCannotWrite() throws Exception {
320336
String body = "Foo";

0 commit comments

Comments
 (0)