diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java index bc26c573f..81271828b 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryEntityController.java @@ -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 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(headers, HttpStatus.OK); } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java index ef34a333b..7d2e56c87 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryEntityControllerIntegrationTests.java @@ -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 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)); + } }