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

@@ -25,6 +25,7 @@ import java.util.Map;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.support.RepositoriesUtils;
import org.springframework.hateoas.RelProvider;
@@ -44,6 +45,7 @@ public class ResourceMappings implements Iterable<ResourceMetadata> {
private final Map<Class<?>, ResourceMetadata> cache = new HashMap<Class<?>, ResourceMetadata>();
private final Map<Class<?>, SearchResourceMappings> searchCache = new HashMap<Class<?>, SearchResourceMappings>();
private final Map<PersistentProperty<?>, ResourceMapping> propertyCache = new HashMap<PersistentProperty<?>, ResourceMapping>();
/**
* Creates a new {@link ResourceMappings} using the given {@link RepositoryRestConfiguration} and {@link Repositories}
@@ -187,7 +189,19 @@ public class ResourceMappings implements Iterable<ResourceMetadata> {
* @see org.springframework.data.rest.core.mapping.ResourceMetadataProvider#getMappingFor(org.springframework.data.mapping.PersistentProperty)
*/
ResourceMapping getMappingFor(PersistentProperty<?> property) {
return getMappingFor(property.getActualType());
ResourceMapping propertyMapping = propertyCache.get(property);
if (propertyMapping != null) {
return propertyMapping;
}
ResourceMetadata propertyTypeMapping = getMappingFor(property.getActualType());
propertyMapping = new PersistentPropertyResourceMapping(property, propertyTypeMapping == null ? false
: propertyTypeMapping.isExported());
propertyCache.put(property, propertyMapping);
return propertyMapping;
}
/*
@@ -208,4 +222,55 @@ public class ResourceMappings implements Iterable<ResourceMetadata> {
public Iterator<ResourceMetadata> iterator() {
return cache.values().iterator();
}
/**
* Special resource mapping for {@link PersistentProperty} instances.
*
* @author Oliver Gierke
*/
private static class PersistentPropertyResourceMapping implements ResourceMapping {
private final PersistentProperty<?> property;
private final boolean exported;
/**
* Creates a new {@link PersistentPropertyResourceMapping}.
*
* @param property must not be {@literal null}.
* @param exported whether the property is exported or not.
*/
public PersistentPropertyResourceMapping(PersistentProperty<?> property, boolean exported) {
Assert.notNull(property, "PersistentProperty must not be null!");
this.property = property;
this.exported = exported;
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getPath()
*/
@Override
public Path getPath() {
return new Path(property.getName());
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.mapping.ResourceMapping#getRel()
*/
@Override
public String getRel() {
return property.getName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.mapping.ResourceMapping#isExported()
*/
@Override
public Boolean isExported() {
return exported;
}
}
}

View File

@@ -24,13 +24,14 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.domain.jpa.CreditCard;
import org.springframework.data.rest.core.domain.jpa.JpaRepositoryConfig;
import org.springframework.data.rest.core.domain.jpa.Person;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@@ -78,4 +79,22 @@ public class ResourceMappingsIntegrationTest {
assertThat(creditCardMapping.isExported(), is(false));
assertThat(creditCardMapping.getSearchResourceMappings().isExported(), is(false));
}
/**
* @see DATAREST-112
*/
@Test
public void usesPropertyNameAsRelForPropertyResourceMapping() {
Repositories repositories = new Repositories(factory);
PersistentEntity<?, ?> entity = repositories.getPersistentEntity(Person.class);
PersistentProperty<?> property = entity.getPersistentProperty("siblings");
ResourceMetadata metadata = mappings.getMappingFor(Person.class);
ResourceMapping mapping = metadata.getMappingFor(property);
assertThat(mapping.getRel(), is("siblings"));
assertThat(mapping.getPath(), is(new Path("siblings")));
assertThat(mapping.isExported(), is(true));
}
}