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.
This commit is contained in:
Oliver Gierke
2017-03-01 21:30:37 +01:00
parent 9a1dafdd24
commit 59b2243864
3 changed files with 15 additions and 6 deletions

View File

@@ -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";

View File

@@ -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());
}
}
}

View File

@@ -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<String> mediaTypes = new LinkedHashSet<String>();
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()]));
}