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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user