diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/MappingResourceMetadata.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/MappingResourceMetadata.java index d21aac9c9..6dfa2f7d6 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/MappingResourceMetadata.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/MappingResourceMetadata.java @@ -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. * diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMapping.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMapping.java index 2b4e78dfe..755a58488 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMapping.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMapping.java @@ -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(); } diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/TypeBasedCollectionResourceMapping.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/TypeBasedCollectionResourceMapping.java index 89f6f02a3..9392fefd8 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/TypeBasedCollectionResourceMapping.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/TypeBasedCollectionResourceMapping.java @@ -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()); } /* diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/MappingResourceMetadataUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/MappingResourceMetadataUnitTests.java index 918ec175e..812fde399 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/MappingResourceMetadataUnitTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/MappingResourceMetadataUnitTests.java @@ -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 { } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMappingUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMappingUnitTests.java index c81dd562c..48d943e30 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMappingUnitTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/PersistentPropertyResourceMappingUnitTests.java @@ -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() { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java index 621fd59cd..c52aa4986 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AssociationLinksUnitTests.java @@ -125,6 +125,7 @@ public class AssociationLinksUnitTests { @RestResource(exported = false) @Reference Property hiddenProperty; } + @RestResource public static class Property { }