From 59b22438649710f07e6c20fe253b4d8f7023f6b9 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 1 Mar 2017 21:30:37 +0100 Subject: [PATCH] DATAREST-1003 - Entity resources don't answer arbitrary JSON requests. Previously, when a request was sending an Accept header of some arbitrary *+json, the request was routed through the controllers and might have ended up producing a PersistentEntityResource that was then mapped using an uncustomized Jackson ObjectMapper. That has caused a huge JSON object to be unfolded which is highly undesirable. We now only answer JSON requests to repository resources that contain an Accept header with any of the explicit JSON media types we got registered. We also now make sure MVC is bootstrapped property for integration tests through the inclusion of DelegatingWebMvcConfiguration. --- .../rest/tests/AbstractWebIntegrationTests.java | 3 ++- .../data/rest/tests/CommonWebTests.java | 13 +++++++++++++ .../rest/webmvc/RepositoryRestHandlerMapping.java | 5 ----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/AbstractWebIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/AbstractWebIntegrationTests.java index cba3785b8..27ed9089d 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/AbstractWebIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/AbstractWebIntegrationTests.java @@ -46,6 +46,7 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration; import com.jayway.jsonpath.InvalidPathException; import com.jayway.jsonpath.JsonPath; @@ -61,7 +62,7 @@ import com.jayway.jsonpath.JsonPath; */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@ContextConfiguration(classes = RepositoryRestMvcConfiguration.class) +@ContextConfiguration(classes = { RepositoryRestMvcConfiguration.class, DelegatingWebMvcConfiguration.class }) public abstract class AbstractWebIntegrationTests { private static final String CONTENT_LINK_JSONPATH = "$._embedded.._links.%s.href"; diff --git a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java index fe7ba0a35..e0842ceeb 100644 --- a/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-core/src/test/java/org/springframework/data/rest/tests/CommonWebTests.java @@ -267,4 +267,17 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { // PATCH to non-existing resource mvc.perform(patch(URI.create(uri))).andExpect(status().isNotFound()); } + + @Test // DATAREST-1003 + public void rejectsUnsupportedAcceptTypeForResources() throws Exception { + + for (String string : expectedRootLinkRels()) { + + Link link = client.discoverUnique(string); + + mvc.perform(get(link.expand().getHref())// + .accept(MediaType.valueOf("application/schema+json")))// + .andExpect(status().isNotAcceptable()); + } + } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java index 7e669eaa6..1ad7c94c8 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java @@ -28,7 +28,6 @@ import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.data.rest.webmvc.support.JpaHelper; import org.springframework.http.MediaType; -import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter; import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -48,9 +47,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl */ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping { - private static final MediaType EVERYTHING_JSON_MEDIA_TYPE = new MediaType("application", "*+json", - AbstractJackson2HttpMessageConverter.DEFAULT_CHARSET); - private final ResourceMappings mappings; private final RepositoryRestConfiguration configuration; @@ -150,7 +146,6 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping { HashSet mediaTypes = new LinkedHashSet(); mediaTypes.add(configuration.getDefaultMediaType().toString()); mediaTypes.add(MediaType.APPLICATION_JSON_VALUE); - mediaTypes.add(EVERYTHING_JSON_MEDIA_TYPE.toString()); return new ProducesRequestCondition(mediaTypes.toArray(new String[mediaTypes.size()])); }