From 556e918ef72db597bdb3c17bb1bb4dec93f13f1b Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 26 Jun 2019 16:34:41 +0200 Subject: [PATCH] DATAREST-1403 - CollectionResourceMapping.getExcerptProjection() now returns Optional. --- .../core/mapping/CollectionResourceMapping.java | 7 +++++-- .../RepositoryAwareResourceMetadata.java | 4 +++- .../RepositoryCollectionResourceMapping.java | 5 +++-- .../TypeBasedCollectionResourceMapping.java | 5 +++-- ...itoryCollectionResourceMappingUnitTests.java | 4 ++-- .../config/ProjectionDefinitionRegistar.java | 17 +++++++++++------ .../webmvc/support/DefaultExcerptProjector.java | 11 ++++++++--- .../PersistentEntityProjectorUnitTests.java | 4 +++- 8 files changed, 38 insertions(+), 19 deletions(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/CollectionResourceMapping.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/CollectionResourceMapping.java index ae6ed9a99..92c69b6dd 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/CollectionResourceMapping.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/CollectionResourceMapping.java @@ -15,6 +15,8 @@ */ package org.springframework.data.rest.core.mapping; +import java.util.Optional; + import org.springframework.hateoas.LinkRelation; /** @@ -40,9 +42,10 @@ public interface CollectionResourceMapping extends ResourceMapping { /** * Returns the projection type to be used when embedding item resources into collections and related resources. If - * {@literal null} is returned this will mean full rendering for collections and no rendering for related resources. + * {@link Optional#empty()} is returned this will mean full rendering for collections and no rendering for related + * resources. * * @return */ - Class getExcerptProjection(); + Optional> getExcerptProjection(); } diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryAwareResourceMetadata.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryAwareResourceMetadata.java index 7961e0412..ac1b960f3 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryAwareResourceMetadata.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryAwareResourceMetadata.java @@ -15,6 +15,8 @@ */ package org.springframework.data.rest.core.mapping; +import java.util.Optional; + import org.springframework.context.annotation.Primary; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.mapping.PersistentEntity; @@ -184,7 +186,7 @@ class RepositoryAwareResourceMetadata implements ResourceMetadata { * @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getExcerptProjection() */ @Override - public Class getExcerptProjection() { + public Optional> getExcerptProjection() { return mapping.getExcerptProjection(); } diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMapping.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMapping.java index ef62df407..c51da953a 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMapping.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMapping.java @@ -84,6 +84,7 @@ class RepositoryCollectionResourceMapping implements CollectionResourceMapping { Class domainType = metadata.getDomainType(); + CollectionResourceMapping domainTypeMapping = EVO_INFLECTOR_IS_PRESENT ? new EvoInflectorTypeBasedCollectionResourceMapping(domainType, relProvider) : new TypeBasedCollectionResourceMapping(domainType, relProvider); @@ -212,8 +213,8 @@ class RepositoryCollectionResourceMapping implements CollectionResourceMapping { * @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getExcerptProjection() */ @Override - public Class getExcerptProjection() { - return excerptProjection.getOptional().orElse(null); + public Optional> getExcerptProjection() { + return excerptProjection.getOptional(); } private static Optional toLinkRelation(Optional source) { 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 2e0f77d54..e34270572 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 @@ -16,6 +16,7 @@ package org.springframework.data.rest.core.mapping; import java.lang.reflect.Modifier; +import java.util.Optional; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.data.rest.core.Path; @@ -164,8 +165,8 @@ class TypeBasedCollectionResourceMapping implements CollectionResourceMapping { * @see org.springframework.data.rest.core.mapping.CollectionResourceMapping#getExcerptProjection() */ @Override - public Class getExcerptProjection() { - return null; + public Optional> getExcerptProjection() { + return Optional.empty(); } /** diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMappingUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMappingUnitTests.java index f10ce35b5..1b6f61295 100755 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMappingUnitTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/RepositoryCollectionResourceMappingUnitTests.java @@ -121,8 +121,8 @@ public class RepositoryCollectionResourceMappingUnitTests { @Test // DATAREST-1401 public void exposesProjectionTypeIfConfigured() { - assertThat(getResourceMappingFor(WithProjection.class).getExcerptProjection()).isEqualTo(Object.class); - assertThat(getResourceMappingFor(WithoutProjection.class).getExcerptProjection()).isNull(); + assertThat(getResourceMappingFor(WithProjection.class).getExcerptProjection()).hasValue(Object.class); + assertThat(getResourceMappingFor(WithoutProjection.class).getExcerptProjection()).isEmpty(); } private static CollectionResourceMapping getResourceMappingFor(Class repositoryInterface) { diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ProjectionDefinitionRegistar.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ProjectionDefinitionRegistar.java index b67b8cdf1..afbff70f7 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ProjectionDefinitionRegistar.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/ProjectionDefinitionRegistar.java @@ -15,6 +15,8 @@ */ package org.springframework.data.rest.webmvc.config; +import java.util.Optional; + import org.springframework.beans.BeansException; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.config.BeanPostProcessor; @@ -68,15 +70,18 @@ public class ProjectionDefinitionRegistar extends InstantiationAwareBeanPostProc for (ResourceMetadata resourceMetadata : mappings) { - Class projection = resourceMetadata.getExcerptProjection(); + Optional> projection = resourceMetadata.getExcerptProjection(); - if (projection != null) { + projection.ifPresent(it -> { - Projection annotation = AnnotationUtils.findAnnotation(projection, Projection.class); - Class[] target = annotation == null ? new Class[] { resourceMetadata.getDomainType() } : annotation.types(); + Projection annotation = AnnotationUtils.findAnnotation(it, Projection.class); - config.getObject().getProjectionConfiguration().addProjection(projection, target); - } + Class[] target = annotation == null // + ? new Class[] { resourceMetadata.getDomainType() } // + : annotation.types(); + + config.getObject().getProjectionConfiguration().addProjection(it, target); + }); } return bean; diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/DefaultExcerptProjector.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/DefaultExcerptProjector.java index dd4b7fd86..5bc22a43c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/DefaultExcerptProjector.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/DefaultExcerptProjector.java @@ -17,6 +17,8 @@ package org.springframework.data.rest.webmvc.support; import lombok.RequiredArgsConstructor; +import java.util.Optional; + import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.data.rest.core.mapping.ResourceMetadata; @@ -45,10 +47,13 @@ public class DefaultExcerptProjector implements ExcerptProjector { Assert.notNull(source, "Projection source must not be null!"); ResourceMetadata metadata = mappings.getMetadataFor(source.getClass()); - Class projection = metadata == null ? null : metadata.getExcerptProjection(); - return projection == null || projection.equals(source.getClass()) ? source - : factory.createProjection(projection, source); + return Optional.ofNullable(metadata) // + .flatMap(ResourceMetadata::getExcerptProjection) // + .filter(it -> !it.equals(source.getClass())) // + . map(it -> factory.createProjection(it, source)) // + .orElse(source); + } /* diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjectorUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjectorUnitTests.java index b6924d376..704bdaed2 100755 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjectorUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjectorUnitTests.java @@ -18,6 +18,8 @@ package org.springframework.data.rest.webmvc.support; import static org.assertj.core.api.Assertions.*; import static org.mockito.Mockito.*; +import java.util.Optional; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,7 +54,7 @@ public class PersistentEntityProjectorUnitTests { ResourceMetadata metadata = mock(ResourceMetadata.class); doReturn(metadata).when(mappings).getMetadataFor(Object.class); - doReturn(Excerpt.class).when(metadata).getExcerptProjection(); + doReturn(Optional.of(Excerpt.class)).when(metadata).getExcerptProjection(); } @Test // DATAREST-221