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.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();
}

View File

@@ -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();
}

View File

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

View File

@@ -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();
}
/**

View File

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