DATAREST-261 - Fixed RepositoryRelProvider registration.

We now register a RepositoryRelProvider that hadn't been exported before so that clients using a RelProvider will automatically see potentially customized rels for repositories.

Changed RepositoryRelProvider to lazily depend on the ResourceMappings as will be requested eagerly for injection into the configuration class itself.
This commit is contained in:
Oliver Gierke
2014-02-27 14:53:24 +01:00
parent f409106139
commit aa5316fb69
4 changed files with 34 additions and 9 deletions

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.data.rest.core.support;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.hateoas.RelProvider;
import org.springframework.util.Assert;
@@ -24,16 +27,17 @@ import org.springframework.util.Assert;
*
* @author Oliver Gierke
*/
@Order(Ordered.LOWEST_PRECEDENCE + 10)
public class RepositoryRelProvider implements RelProvider {
private final ResourceMappings mappings;
private final ObjectFactory<ResourceMappings> mappings;
/**
* Creates a new {@link RepositoryRelProvider} for the given {@link ResourceMappings}.
*
* @param mappings must not be {@literal null}.
*/
public RepositoryRelProvider(ResourceMappings mappings) {
public RepositoryRelProvider(ObjectFactory<ResourceMappings> mappings) {
Assert.notNull(mappings, "ResourceMappings must not be null!");
this.mappings = mappings;
@@ -45,7 +49,7 @@ public class RepositoryRelProvider implements RelProvider {
*/
@Override
public String getCollectionResourceRelFor(Class<?> type) {
return mappings.getMappingFor(type).getRel();
return mappings.getObject().getMappingFor(type).getRel();
}
/*
@@ -54,7 +58,7 @@ public class RepositoryRelProvider implements RelProvider {
*/
@Override
public String getItemResourceRelFor(Class<?> type) {
return mappings.getMappingFor(type).getItemResourceRel();
return mappings.getObject().getMappingFor(type).getItemResourceRel();
}
/*
@@ -63,6 +67,6 @@ public class RepositoryRelProvider implements RelProvider {
*/
@Override
public boolean supports(Class<?> delimiter) {
return mappings.hasMappingFor(delimiter);
return mappings.getObject().hasMappingFor(delimiter);
}
}

View File

@@ -45,6 +45,7 @@ import org.springframework.data.rest.core.invoke.RepositoryInvokerFactory;
import org.springframework.data.rest.core.mapping.ResourceDescription;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.support.DomainObjectMerger;
import org.springframework.data.rest.core.support.RepositoryRelProvider;
import org.springframework.data.rest.core.util.UUIDConverter;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.PersistentEntityResourceHandlerMethodArgumentResolver;
@@ -132,6 +133,11 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
return new Repositories(beanFactory);
}
@Bean
public RepositoryRelProvider repositoryRelProvider(ObjectFactory<ResourceMappings> resourceMappings) {
return new RepositoryRelProvider(resourceMappings);
}
@Bean
@Qualifier
public DefaultFormattingConversionService defaultConversionService() {
@@ -418,7 +424,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
Repositories repositories = repositories();
RepositoryRestConfiguration config = config();
return new ResourceMappings(config, repositories, getDefaultedRelProvider());
return new ResourceMappings(config, repositories);
}
/**

View File

@@ -270,8 +270,13 @@ public abstract class AbstractWebIntegrationTests {
protected void assertJsonPathDoesntExist(String path, MockHttpServletResponse response) throws Exception {
try {
JsonPath.read(response.getContentAsString(), path);
fail(path + " should have failed");
Object result = JsonPath.read(response.getContentAsString(), path);
if (result != null) {
fail("Was expecting to find no value for path " + path + " but got " + result.toString());
}
} catch (InvalidPathException e) {}
}

View File

@@ -33,6 +33,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.RelProvider;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
@@ -60,6 +61,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests {
@Autowired TestDataPopulator loader;
@Autowired ResourceMappings mappings;
@Autowired RelProvider relProvider;
ObjectMapper mapper = new ObjectMapper();
@@ -429,6 +431,14 @@ public class JpaWebTests extends AbstractWebIntegrationTests {
mvc.perform(post(link.getHref()).content("{}").contentType(MediaType.APPLICATION_JSON)).//
andExpect(status().isMethodNotAllowed());
}
/**
* @see DATAREST-261
*/
@Test
public void relProviderDetectsCustomizedMapping() {
assertThat(relProvider.getCollectionResourceRelFor(Person.class), is("people"));
}
/**
* Asserts the {@link Person} resource the given link points to contains siblings with the given names.
@@ -440,7 +450,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests {
private void assertSiblingNames(Link link, String... siblingNames) throws Exception {
String responseBody = request(link).getContentAsString();
List<String> persons = JsonPath.read(responseBody, "$._embedded.persons[*].firstName");
List<String> persons = JsonPath.read(responseBody, "$._embedded.people[*].firstName");
assertThat(persons, hasSize(siblingNames.length));
assertThat(persons, hasItems(siblingNames));