DATAREST-1076 - Expose constructor that takes RelProvider of RepositoryResourceMappings.
We now expose the constructor that takes a RelProvider in RepositoryResourceMappings so that clients can tweak the default relation names. Changed the order of constructor parameters of (previously) non-public constructors for consistency. The RelProvider to be used with the mappings can now be configured via RepositoryRestConfiguration and defaults to the EvoInflector based one.
This commit is contained in:
@@ -15,6 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.rest.core.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -25,6 +29,8 @@ import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy;
|
||||
import org.springframework.data.rest.core.mapping.RepositoryDetectionStrategy.RepositoryDetectionStrategies;
|
||||
import org.springframework.data.rest.core.support.EntityLookup;
|
||||
import org.springframework.hateoas.MediaTypes;
|
||||
import org.springframework.hateoas.RelProvider;
|
||||
import org.springframework.hateoas.core.EvoInflectorRelProvider;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -60,6 +66,11 @@ public class RepositoryRestConfiguration {
|
||||
private ResourceMappingConfiguration repoMappings = new ResourceMappingConfiguration();
|
||||
private RepositoryDetectionStrategy repositoryDetectionStrategy = RepositoryDetectionStrategies.DEFAULT;
|
||||
|
||||
/**
|
||||
* The {@link RelProvider} to be used to calculate the link relation defaults for repositories.
|
||||
*/
|
||||
private @Getter @Setter @NonNull RelProvider relProvider = new EvoInflectorRelProvider();
|
||||
|
||||
private final RepositoryCorsRegistry corsRegistry = new RepositoryCorsRegistry();
|
||||
private final ProjectionDefinitionConfiguration projectionConfiguration;
|
||||
private final MetadataConfiguration metadataConfiguration;
|
||||
|
||||
@@ -47,19 +47,19 @@ class RepositoryCollectionResourceMapping implements CollectionResourceMapping {
|
||||
private final RepositoryMetadata metadata;
|
||||
|
||||
public RepositoryCollectionResourceMapping(RepositoryMetadata metadata, RepositoryDetectionStrategy strategy) {
|
||||
this(metadata, new EvoInflectorRelProvider(), strategy);
|
||||
this(metadata, strategy, new EvoInflectorRelProvider());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link RepositoryCollectionResourceMapping} for the given repository using the given
|
||||
* {@link RelProvider}.
|
||||
*
|
||||
* @param repositoryType must not be {@literal null}.
|
||||
* @param relProvider must not be {@literal null}.
|
||||
* @param strategy must not be {@literal null}.
|
||||
* @param relProvider must not be {@literal null}.
|
||||
* @param repositoryType must not be {@literal null}.
|
||||
*/
|
||||
RepositoryCollectionResourceMapping(RepositoryMetadata metadata, RelProvider relProvider,
|
||||
RepositoryDetectionStrategy strategy) {
|
||||
RepositoryCollectionResourceMapping(RepositoryMetadata metadata, RepositoryDetectionStrategy strategy,
|
||||
RelProvider relProvider) {
|
||||
|
||||
Assert.notNull(metadata, "Repository metadata must not be null!");
|
||||
Assert.notNull(relProvider, "RelProvider must not be null!");
|
||||
|
||||
@@ -52,7 +52,7 @@ public class RepositoryResourceMappings extends PersistentEntitiesResourceMappin
|
||||
*/
|
||||
public RepositoryResourceMappings(Repositories repositories, PersistentEntities entities,
|
||||
RepositoryDetectionStrategy strategy) {
|
||||
this(repositories, entities, new EvoInflectorRelProvider(), strategy);
|
||||
this(repositories, entities, strategy, new EvoInflectorRelProvider());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,11 +61,11 @@ public class RepositoryResourceMappings extends PersistentEntitiesResourceMappin
|
||||
*
|
||||
* @param repositories must not be {@literal null}.
|
||||
* @param entities must not be {@literal null}.
|
||||
* @param relProvider must not be {@literal null}.
|
||||
* @param strategy must not be {@literal null}.
|
||||
* @param relProvider must not be {@literal null}.
|
||||
*/
|
||||
RepositoryResourceMappings(Repositories repositories, PersistentEntities entities, RelProvider relProvider,
|
||||
RepositoryDetectionStrategy strategy) {
|
||||
public RepositoryResourceMappings(Repositories repositories, PersistentEntities entities, RepositoryDetectionStrategy strategy,
|
||||
RelProvider relProvider) {
|
||||
|
||||
super(entities);
|
||||
|
||||
@@ -85,8 +85,8 @@ public class RepositoryResourceMappings extends PersistentEntitiesResourceMappin
|
||||
Class<?> repositoryInterface = repositoryInformation.getRepositoryInterface();
|
||||
PersistentEntity<?, ?> entity = repositories.getPersistentEntity(type);
|
||||
|
||||
CollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(repositoryInformation, provider,
|
||||
strategy);
|
||||
CollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(repositoryInformation, strategy,
|
||||
provider);
|
||||
RepositoryAwareResourceMetadata information = new RepositoryAwareResourceMetadata(entity, mapping, this,
|
||||
repositoryInformation);
|
||||
|
||||
|
||||
@@ -132,4 +132,9 @@ public class RepositoryRestConfigurationUnitTests {
|
||||
CorsConfiguration corsConfiguration = corsConfigurations.get("/hello");
|
||||
assertThat(corsConfiguration.getMaxAge(), is(1234L));
|
||||
}
|
||||
|
||||
@Test // DATAREST-1076
|
||||
public void rejectsNullRelProvider() {
|
||||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> configuration.setRelProvider(null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ public class RepositoryResourceMappingsIntegrationTests {
|
||||
|
||||
Repositories repositories = new Repositories(factory);
|
||||
this.mappings = new RepositoryResourceMappings(repositories, new PersistentEntities(Arrays.asList(mappingContext)),
|
||||
new EvoInflectorRelProvider(), RepositoryDetectionStrategies.DEFAULT);
|
||||
RepositoryDetectionStrategies.DEFAULT, new EvoInflectorRelProvider());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -616,7 +616,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
|
||||
@Bean
|
||||
public RepositoryResourceMappings resourceMappings() {
|
||||
return new RepositoryResourceMappings(repositories(), persistentEntities(),
|
||||
config().getRepositoryDetectionStrategy());
|
||||
config().getRepositoryDetectionStrategy(), config().getRelProvider());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user