DATAREST-112 - Moved property mapping into PropertyResourceMapping.

PersistentEntityJackson2Module now entirely relies on the property mapping to do the right thing for link creation. The PropertyResourceMapping now returns the property name for both rel and path.
This commit is contained in:
Oliver Gierke
2013-08-21 10:20:37 +02:00
parent 70443fc6a7
commit 252efc5196
4 changed files with 95 additions and 13 deletions

View File

@@ -27,6 +27,7 @@ import org.springframework.data.mapping.model.BeanWrapper;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.UriDomainClassConverter;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.ResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.webmvc.PersistentEntityResource;
@@ -72,19 +73,16 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init
PersistentProperty<?> persistentProperty, List<Link> links) {
Assert.isTrue(persistentProperty.isAssociation(), "PersistentProperty must be an association!");
ResourceMetadata metadata = mappings.getMappingFor(persistentProperty.getOwner().getType());
ResourceMetadata ownerMetadata = mappings.getMappingFor(persistentProperty.getOwner().getType());
if (!metadata.isManagedResource(persistentProperty)) {
if (!ownerMetadata.isManagedResource(persistentProperty)) {
return false;
}
metadata = mappings.getMappingFor(persistentProperty.getActualType());
if (metadata.isExported()) {
String propertyRel = String.format("%s.%s", metadata.getSingleResourceRel(), persistentProperty.getName());
links.add(builder.slash(persistentProperty.getName()).withRel(propertyRel));
ResourceMapping propertyMapping = ownerMetadata.getMappingFor(persistentProperty);
if (propertyMapping.isExported()) {
links.add(builder.slash(propertyMapping.getPath()).withRel(propertyMapping.getRel()));
// This is an association. We added a Link.
return true;
}

View File

@@ -68,10 +68,10 @@ public class PersistentEntitySerializationTests {
String s = writer.toString();
Link fatherLink = linkDiscoverer.findLinkWithRel("person.father", s);
Link fatherLink = linkDiscoverer.findLinkWithRel("father", s);
assertThat(fatherLink.getHref(), endsWith(new UriTemplate("/{id}/father").expand(person.getId()).toString()));
Link siblingLink = linkDiscoverer.findLinkWithRel("person.siblings", s);
Link siblingLink = linkDiscoverer.findLinkWithRel("siblings", s);
assertThat(siblingLink.getHref(), endsWith(new UriTemplate("/{id}/siblings").expand(person.getId()).toString()));
}
}