Skip to content

Commit

Permalink
Support "Accept-Patch" for unsupported media type
Browse files Browse the repository at this point in the history
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 spring-projectsgh-26759
  • Loading branch information
poutsma authored and Zoran0104 committed Aug 20, 2021
1 parent 9c55ed3 commit 849d1c8
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 6 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
Expand All @@ -20,9 +20,12 @@
import java.util.List;

import org.springframework.core.ResolvableType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

/**
* Exception for errors that fit response status 415 (unsupported media type).
Expand All @@ -41,6 +44,9 @@ public class UnsupportedMediaTypeStatusException extends ResponseStatusException
@Nullable
private final ResolvableType bodyType;

@Nullable
private final HttpMethod method;


/**
* Constructor for when the specified Content-Type is invalid.
Expand All @@ -50,13 +56,14 @@ public UnsupportedMediaTypeStatusException(@Nullable String reason) {
this.contentType = null;
this.supportedMediaTypes = Collections.emptyList();
this.bodyType = null;
this.method = null;
}

/**
* Constructor for when the Content-Type can be parsed but is not supported.
*/
public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes) {
this(contentType, supportedTypes, null);
this(contentType, supportedTypes, null, null);
}

/**
Expand All @@ -65,11 +72,30 @@ public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List
*/
public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes,
@Nullable ResolvableType bodyType) {
this(contentType, supportedTypes, bodyType, null);
}

/**
* Constructor that provides the HTTP method.
* @since 5.3.6
*/
public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes,
@Nullable HttpMethod method) {
this(contentType, supportedTypes, null, method);
}

/**
* Constructor for when trying to encode from or decode to a specific Java type.
* @since 5.3.6
*/
public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes,
@Nullable ResolvableType bodyType, @Nullable HttpMethod method) {

super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, initReason(contentType, bodyType));
this.contentType = contentType;
this.supportedMediaTypes = Collections.unmodifiableList(supportedTypes);
this.bodyType = bodyType;
this.method = method;
}

private static String initReason(@Nullable MediaType contentType, @Nullable ResolvableType bodyType) {
Expand Down Expand Up @@ -107,4 +133,14 @@ public ResolvableType getBodyType() {
return this.bodyType;
}

@Override
public HttpHeaders getResponseHeaders() {
if (HttpMethod.PATCH != this.method || CollectionUtils.isEmpty(this.supportedMediaTypes) ) {
return HttpHeaders.EMPTY;
}
HttpHeaders headers = new HttpHeaders();
headers.setAcceptPatch(this.supportedMediaTypes);
return headers;
}

}
Expand Up @@ -190,7 +190,7 @@ protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
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()) {
Expand Down
Expand Up @@ -317,6 +317,26 @@ public void handleMatchMatrixVariablesDecoding() {
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) {
Expand Down
@@ -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.
Expand Down Expand Up @@ -231,6 +231,12 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(
List<MediaType> mediaTypes = ex.getSupportedMediaTypes();
if (!CollectionUtils.isEmpty(mediaTypes)) {
headers.setAccept(mediaTypes);
if (request instanceof ServletWebRequest) {
ServletWebRequest servletWebRequest = (ServletWebRequest) request;
if (HttpMethod.PATCH.equals(servletWebRequest.getHttpMethod())) {
headers.setAcceptPatch(mediaTypes);
}
}
}

return handleExceptionInternal(ex, null, headers, status, request);
Expand Down
Expand Up @@ -281,6 +281,9 @@ protected ModelAndView handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupported
List<MediaType> mediaTypes = ex.getSupportedMediaTypes();
if (!CollectionUtils.isEmpty(mediaTypes)) {
response.setHeader("Accept", MediaType.toString(mediaTypes));
if (request.getMethod().equals("PATCH")) {
response.setHeader("Accept-Patch", MediaType.toString(mediaTypes));
}
}
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
return new ModelAndView();
Expand Down
@@ -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.
Expand Down Expand Up @@ -113,6 +113,20 @@ public void handleHttpMediaTypeNotSupported() {

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
Expand Down
@@ -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.
Expand Down Expand Up @@ -976,6 +976,26 @@ void unsupportedRequestBody(boolean usePathPatterns) throws Exception {
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);
Expand Down
Expand Up @@ -87,6 +87,18 @@ public void handleHttpMediaTypeNotSupported() {
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);
Expand Down

0 comments on commit 849d1c8

Please sign in to comment.