DATAREST-333, DATAREST-348 - OPTIONS requests now expose Accept-Patch header.

As recommended in RFC 5789, the support for PATCH request should be advertised in OPTIONS requests (already in place) and include an Accept-Patch header listing the patch media types supported. We now include the media types for JSON Patch, JSON Merge Patch and plain JSON in that header.

[0] http://tools.ietf.org/html/rfc5789#section-3
This commit is contained in:
Oliver Gierke
2014-07-09 20:39:03 +02:00
parent 020de45c1b
commit b29fea84e9
2 changed files with 27 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ import static org.springframework.http.HttpMethod.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -53,6 +54,7 @@ import org.springframework.hateoas.UriTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -68,6 +70,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
class RepositoryEntityController extends AbstractRepositoryRestController implements ApplicationEventPublisherAware {
private static final String BASE_MAPPING = "/{repository}";
private static final List<String> ACCEPT_PATCH_HEADERS = Arrays.asList(//
RestMediaTypes.MERGE_PATCH_JSON.toString(), //
RestMediaTypes.JSON_PATCH_JSON.toString(), //
MediaType.APPLICATION_JSON_VALUE);
private final RepositoryEntityLinks entityLinks;
private final RepositoryRestConfiguration config;
@@ -238,6 +244,7 @@ class RepositoryEntityController extends AbstractRepositoryRestController implem
HttpHeaders headers = new HttpHeaders();
headers.setAllow(information.getSupportedMethods(ResourceType.ITEM));
headers.put("Accept-Patch", ACCEPT_PATCH_HEADERS);
return new ResponseEntity<Object>(headers, HttpStatus.OK);
}

View File

@@ -20,6 +20,8 @@ import static org.junit.Assert.*;
import static org.springframework.data.rest.webmvc.WebTestUtils.*;
import static org.springframework.http.HttpMethod.*;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mapping.context.PersistentEntities;
@@ -32,6 +34,7 @@ import org.springframework.data.rest.webmvc.jpa.Order;
import org.springframework.data.rest.webmvc.jpa.Person;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
@@ -157,4 +160,21 @@ public class RepositoryEntityControllerIntegrationTests extends AbstractControll
HttpEntity<?> response = controller.optionsForItemResource(getResourceInformation(Person.class));
assertAllowHeaders(response, GET, PUT, PATCH, DELETE, HEAD, OPTIONS);
}
/**
* @see DATAREST-333, DATAREST-348
*/
@Test
public void optionsForItermResourceSetsAllowPatchHeader() {
ResponseEntity<?> entity = controller.optionsForItemResource(getResourceInformation(Person.class));
List<String> value = entity.getHeaders().get("Accept-Patch");
assertThat(value, hasSize(3));
assertThat(value, hasItems(//
RestMediaTypes.JSON_PATCH_JSON.toString(), //
RestMediaTypes.MERGE_PATCH_JSON.toString(), //
MediaType.APPLICATION_JSON_VALUE));
}
}