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

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@@ -113,6 +113,20 @@ public class ResponseEntityExceptionHandlerTests {
ResponseEntity<Object> responseEntity = testException(ex);
assertThat(responseEntity.getHeaders().getAccept()).isEqualTo(acceptable);
assertThat(responseEntity.getHeaders().getAcceptPatch()).isEmpty();
}
@Test
public void patchHttpMediaTypeNotSupported() {
this.servletRequest = new MockHttpServletRequest("PATCH", "/");
this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);
List<MediaType> acceptable = Arrays.asList(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML);
Exception ex = new HttpMediaTypeNotSupportedException(MediaType.APPLICATION_JSON, acceptable);
ResponseEntity<Object> responseEntity = testException(ex);
assertThat(responseEntity.getHeaders().getAccept()).isEqualTo(acceptable);
assertThat(responseEntity.getHeaders().getAcceptPatch()).isEqualTo(acceptable);
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@@ -976,6 +976,26 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
assertThat(response.getHeader("Accept")).isEqualTo("text/plain");
}
@PathPatternsParameterizedTest
void unsupportedPatchBody(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("PATCH", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "application/pdf");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(415);
assertThat(response.getHeader("Accept-Patch")).isEqualTo("text/plain");
}
@PathPatternsParameterizedTest
void responseBodyNoAcceptHeader(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns);

View File

@@ -87,6 +87,18 @@ public class DefaultHandlerExceptionResolverTests {
assertThat(response.getHeader("Accept")).as("Invalid Accept header").isEqualTo("application/pdf");
}
@Test
public void patchHttpMediaTypeNotSupported() {
HttpMediaTypeNotSupportedException ex = new HttpMediaTypeNotSupportedException(new MediaType("text", "plain"),
Collections.singletonList(new MediaType("application", "pdf")));
MockHttpServletRequest request = new MockHttpServletRequest("PATCH", "/");
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
assertThat(mav).as("No ModelAndView returned").isNotNull();
assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
assertThat(response.getStatus()).as("Invalid status code").isEqualTo(415);
assertThat(response.getHeader("Accept-Patch")).as("Invalid Accept header").isEqualTo("application/pdf");
}
@Test
public void handleMissingPathVariable() throws NoSuchMethodException {
Method method = getClass().getMethod("handle", String.class);