DATAREST-1403 - CollectionResourceMapping.getExcerptProjection() now returns Optional.

This commit is contained in:
Oliver Drotbohm
2019-06-26 16:34:41 +02:00
parent 2d994c7076
commit 556e918ef7
8 changed files with 38 additions and 19 deletions

View File

@@ -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<Class<?>> 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;

View File

@@ -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())) //
.<Object> map(it -> factory.createProjection(it, source)) //
.orElse(source);
}
/*

View File

@@ -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