DATAREST-577 - Excerpt projections are now configured automatically.

Excerpt projections defined in @RepositoryRestResource had to be discoverable (i.e. located in a sub-package of the domain type). RepositoryRestMvcConfiguration now uses an newly introduced constructor of ProjectionDefinitionConfiguration that automatically registers all projects in the given ResourceMappings.
This commit is contained in:
Oliver Gierke
2015-06-11 20:12:40 +02:00
parent 5f091a68f8
commit 3e0402b70b
3 changed files with 49 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,11 +15,13 @@
*/
package org.springframework.data.rest.core.config;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.core.projection.ProjectionDefinitions;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -38,8 +40,32 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions
private final Map<ProjectionDefinitionKey, Class<?>> projectionDefinitions;
private String parameterName = DEFAULT_PROJECTION_PARAMETER_NAME;
/**
* Creates a new {@link ProjectionDefinitionConfiguration}.
*/
public ProjectionDefinitionConfiguration() {
this(Collections.<ResourceMetadata> emptySet());
}
/**
* Creates a new {@link ProjectionDefinitionConfiguration} from the given {@link ResourceMetadata} instances.
*
* @param resourceMetadata must not be {@literal null}.
*/
public ProjectionDefinitionConfiguration(Iterable<ResourceMetadata> resourceMetadata) {
Assert.notNull(resourceMetadata, "ResourceMetadata must not be null!");
this.projectionDefinitions = new HashMap<ProjectionDefinitionKey, Class<?>>();
for (ResourceMetadata metadata : resourceMetadata) {
Class<?> projection = metadata.getExcerptProjection();
if (projection != null) {
addProjection(projection);
}
}
}
/*
@@ -48,7 +74,7 @@ public class ProjectionDefinitionConfiguration implements ProjectionDefinitions
*/
public String getParameterName() {
return parameterName;
};
}
/**
* Configures the request parameter name to be used to accept the projection name to be returned.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,14 @@ package org.springframework.data.rest.core.config;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration.ProjectionDefinitionKey;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
/**
* Unit tests for {@link ProjectionDefinitionConfiguration}.
@@ -150,11 +155,22 @@ public class ProjectionDefinitionConfigurationUnitTests {
is(typeCompatibleWith(ParentProjection.class)));
}
@Projection(name = "name", types = Integer.class)
interface SampleProjection {
/**
* @see DATAREST-577
*/
@Test
public void registersExcerptProjectionsByDefault() {
ResourceMetadata metadata = mock(ResourceMetadata.class);
doReturn(SampleProjection.class).when(metadata).getExcerptProjection();
assertThat(new ProjectionDefinitionConfiguration(Arrays.asList(metadata)).getProjectionsFor(Integer.class),
Matchers.<String, Class<?>> hasEntry("name", SampleProjection.class));
}
@Projection(name = "name", types = Integer.class)
interface SampleProjection {}
@Projection(types = Integer.class)
interface Default {

View File

@@ -217,8 +217,9 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
@Bean
public RepositoryRestConfiguration config() {
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration();
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration(resourceMappings());
// Register projections found in packages
for (Class<?> projection : getProjections(repositories())) {
configuration.addProjection(projection);
}