DATAREST-518 - MappingResourceMetadata is now not exported by default.

This commit is contained in:
Oliver Gierke
2015-04-14 21:56:03 +02:00
parent 85ced098c6
commit 3537bbd20b
6 changed files with 56 additions and 9 deletions

View File

@@ -24,6 +24,7 @@ import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.SimpleAssociationHandler;
import org.springframework.data.mapping.SimplePropertyHandler;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.mapping.SupportedHttpMethods.NoSupportedMethods;
import org.springframework.util.Assert;
@@ -37,6 +38,7 @@ class MappingResourceMetadata extends TypeBasedCollectionResourceMapping impleme
private final PersistentEntity<?, ?> entity;
private final PropertyMappings propertyMappings;
private final boolean explicitlyExported;
/**
* Creates a new {@link MappingResourceMetadata} for the given {@link PersistentEntity}.
@@ -49,6 +51,9 @@ class MappingResourceMetadata extends TypeBasedCollectionResourceMapping impleme
this.entity = entity;
this.propertyMappings = new PropertyMappings(resourceMappings);
RestResource annotation = entity.findAnnotation(RestResource.class);
this.explicitlyExported = annotation != null && annotation.exported();
}
public MappingResourceMetadata init() {
@@ -113,6 +118,15 @@ class MappingResourceMetadata extends TypeBasedCollectionResourceMapping impleme
return propertyMappings.getMappingFor(mappedPath);
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.mapping.TypeBasedCollectionResourceMapping#isExported()
*/
@Override
public boolean isExported() {
return explicitlyExported;
}
/**
* Value object for {@link ResourceMapping}s for {@link PersistentProperty} instances.
*

View File

@@ -76,6 +76,10 @@ class PersistentPropertyResourceMapping implements PropertyAwareResourceMapping
@Override
public boolean isExported() {
if (!property.isAssociation()) {
return false;
}
ResourceMapping typeMapping = mappings.getMetadataFor(property.getActualType());
return !typeMapping.isExported() ? false : annotation == null ? true : annotation.exported();
}

View File

@@ -83,7 +83,7 @@ class TypeBasedCollectionResourceMapping implements CollectionResourceMapping {
*/
@Override
public boolean isExported() {
return annotation == null ? Modifier.isPublic(type.getModifiers()) : annotation.exported();
return annotation != null ? annotation.exported() : Modifier.isPublic(type.getModifiers());
}
/*

View File

@@ -31,6 +31,8 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.rest.core.annotation.RestResource;
/**
* Unit tests for {@link MappingResourceMetadata}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
@@ -38,17 +40,19 @@ public class MappingResourceMetadataUnitTests {
MongoMappingContext context = new MongoMappingContext();
MongoPersistentEntity<?> entity = context.getPersistentEntity(Entity.class);
ResourceMappings resourceMappings = new PersistentEntitiesResourceMappings(new PersistentEntities(
Arrays.asList(context)));
MappingResourceMetadata metadata = new MappingResourceMetadata(entity, resourceMappings).init();
/**
* @see DATAREST-514
*/
@Test
public void allowsLookupOfPropertyByMappedName() {
ResourceMappings resourceMappings = new PersistentEntitiesResourceMappings(new PersistentEntities(
Arrays.asList(context)));
MongoPersistentEntity<?> entity = context.getPersistentEntity(Entity.class);
MongoPersistentProperty property = entity.getPersistentProperty("related");
MappingResourceMetadata metadata = new MappingResourceMetadata(entity, resourceMappings).init();
PropertyAwareResourceMapping propertyMapping = metadata.getProperty("foo");
assertThat(propertyMapping, is(notNullValue()));
@@ -56,10 +60,31 @@ public class MappingResourceMetadataUnitTests {
assertThat(metadata.getMappingFor(property).getPath().matches("foo"), is(true));
}
/**
* @see DATAREST-518
*/
@Test
public void isNotExportedByDefault() {
assertThat(metadata.isExported(), is(false));
}
/**
* @see DATAREST-518
*/
@Test
public void isExportedIfExplicitlyAnnotated() {
MappingResourceMetadata metadata = new MappingResourceMetadata(context.getPersistentEntity(Related.class),
resourceMappings).init();
assertThat(metadata.isExported(), is(true));
}
static class Entity {
@DBRef @RestResource(rel = "foo", path = "foo") private Related related;
}
@RestResource
static class Related {
}

View File

@@ -54,7 +54,7 @@ public class PersistentPropertyResourceMappingUnitTests {
assertThat(mapping, is(notNullValue()));
assertThat(mapping.getPath(), is(new Path("first")));
assertThat(mapping.getRel(), is("first"));
assertThat(mapping.isExported(), is(true));
assertThat(mapping.isExported(), is(false));
}
/**
@@ -85,6 +85,9 @@ public class PersistentPropertyResourceMappingUnitTests {
assertThat(mapping.isExported(), is(false));
}
/**
* @see DATAREST-233
*/
@Test
public void returnsDefaultDescriptionKey() {
@@ -97,7 +100,7 @@ public class PersistentPropertyResourceMappingUnitTests {
}
/**
* @see DATAREST-???
* @see DATAREST-233
*/
@Test
public void considersAtDescription() {

View File

@@ -125,6 +125,7 @@ public class AssociationLinksUnitTests {
@RestResource(exported = false) @Reference Property hiddenProperty;
}
@RestResource
public static class Property {
}