From c682ebbd1d949b257a21709fba2fcc07078aa5ab Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 7 Jul 2021 16:20:09 +0200 Subject: [PATCH] RepositoryResourceMappings now processes all entities to detect repository mappings. We now consider all registered PersistentEntity instances and try to detect repository metadata for them. A case we didn't cover before was that a repository was declared for an aggregate super type but the actual child aggregate class didn't have a dedicated repository declared. While this is a perfectly valid scenario, the mapping information was broken as it fell back on the plain domain type information and produced paths and relation names derived from that, even if there's a super type repository available. Thus, solely traversing the aggregate types we have repositories registered for is not enough. We now traverse all known PersistentEntity types and also register repository metadata for all types that are assignable to a known domain type. The latter is actually implemented in Spring Data Commons' Repositories via spring-projects/spring-data-commons#2406. --- .../core/mapping/RepositoryResourceMappings.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java index 72f95792b..db2103430 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/mapping/RepositoryResourceMappings.java @@ -61,16 +61,21 @@ public class RepositoryResourceMappings extends PersistentEntitiesResourceMappin this.repositories = repositories; this.configuration = configuration; - this.populateCache(repositories, configuration); + this.populateCache(entities, configuration); } - private void populateCache(Repositories repositories, RepositoryRestConfiguration configuration) { + private void populateCache(PersistentEntities entities, RepositoryRestConfiguration configuration) { - for (Class type : repositories) { + for (PersistentEntity> entity : entities) { + + Class type = entity.getType(); + + if (!repositories.hasRepositoryFor(type)) { + continue; + } RepositoryInformation repositoryInformation = repositories.getRequiredRepositoryInformation(type); Class repositoryInterface = repositoryInformation.getRepositoryInterface(); - PersistentEntity entity = repositories.getPersistentEntity(type); RepositoryDetectionStrategy strategy = configuration.getRepositoryDetectionStrategy(); LinkRelationProvider provider = configuration.getRelProvider();