Support "Accept-Patch" for unsupported media type

This commit introduces support in both servlet and webflux for the
"Accept-Patch" header, which is sent when the client sends unsupported
data in PATCH requests.
See  section 2.2 of RFC 5789.

Closes gh-26759
This commit is contained in:
Arjen Poutsma
2021-04-08 14:26:24 +02:00
parent 97f3846971
commit a2d91a562d
8 changed files with 117 additions and 6 deletions

View File

@@ -190,7 +190,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
catch (InvalidMediaTypeException ex) {
throw new UnsupportedMediaTypeStatusException(ex.getMessage());
}
throw new UnsupportedMediaTypeStatusException(contentType, new ArrayList<>(mediaTypes));
throw new UnsupportedMediaTypeStatusException(contentType, new ArrayList<>(mediaTypes), exchange.getRequest().getMethod());
}
if (helper.hasProducesMismatch()) {

View File

@@ -317,6 +317,26 @@ public class RequestMappingInfoHandlerMappingTests {
assertThat(uriVariables.get("cars")).isEqualTo("cars");
}
@Test
public void handlePatchUnsupportedMediaType() {
MockServerHttpRequest request = MockServerHttpRequest.patch("/qux")
.header("content-type", "application/xml")
.build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
StepVerifier.create(mono)
.expectErrorSatisfies(ex -> {
assertThat(ex).isInstanceOf(UnsupportedMediaTypeStatusException.class);
UnsupportedMediaTypeStatusException umtse = (UnsupportedMediaTypeStatusException) ex;
MediaType mediaType = new MediaType("foo", "bar");
assertThat(umtse.getSupportedMediaTypes()).containsExactly(mediaType);
assertThat(umtse.getResponseHeaders().getAcceptPatch()).containsExactly(mediaType);
})
.verify();
}
@SuppressWarnings("unchecked")
private <T> void assertError(Mono<Object> mono, final Class<T> exceptionClass, final Consumer<T> consumer) {