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:
@@ -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()) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user