DATAREST-1403 - CollectionResourceMapping.getExcerptProjection() now returns Optional.
This commit is contained in:
@@ -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<Class<?>> getExcerptProjection();
|
||||
}
|
||||
|
||||
@@ -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<Class<?>> getExcerptProjection() {
|
||||
return mapping.getExcerptProjection();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Class<?>> getExcerptProjection() {
|
||||
return excerptProjection.getOptional();
|
||||
}
|
||||
|
||||
private static Optional<LinkRelation> toLinkRelation(Optional<String> source) {
|
||||
|
||||
@@ -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<Class<?>> getExcerptProjection() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user