Open alienisty opened 2 years ago
I found that we can workaround the problem by giving SDR more information using a RepositoryRestConfigurer:
/**
* Custom configuration for working around https://github.com/spring-projects/spring-data-rest/issues/2170
*/
@Configuration
public class PolymorphicRepositorySupportConfig implements RepositoryRestConfigurer {
private final JpaMetamodelMappingContext jpaContext;
private final Repositories repositories;
private final Collection<EntityManagerFactory> entityFactories;
public PolymorphicRepositorySupportConfig(JpaMetamodelMappingContext jpaContext,
Repositories repositories,
Collection<EntityManagerFactory> entityFactories) {
this.jpaContext = jpaContext;
this.repositories = repositories;
this.entityFactories = entityFactories;
}
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
entityFactories.stream()
.map(EntityManagerFactory::getMetamodel)
.flatMap(metamodel -> metamodel.getManagedTypes().stream()) // Gets all mapped entities detected by the factories
.map(ManagedType::getJavaType)
.filter(repositories::hasRepositoryFor) // A polymorphic entity will have a repository if any of its ancestors defines one
.forEach(jpaContext::getRequiredPersistentEntity); // Ensure there is a PersistentEntityImpl for any such entity
}
}
If I define a hierarchy of entities, and a polymorphic repository only for the root entity, descendants of the root entity are included under the management of the polymorphic repository only if it exists at least another entity (with a repository) with a linkable association to the specific subclass.
For example, given the following:
then, ResidentialAddress will be handled by the AddressRepository and its rels and href will reflect the ones specified on the AddressRepository. The BusinessAddress, instead, while still returned by the AddressRepository, it will have rels and href generated using the Evo Inflector, which returns an invalid href for the links (businessAddress) which is not mapped and you will get a 500 if you tried to retrieve it under the link specified URL.
The following is an example of the payload returned by querying the collection resource '/addresses':
My expectation is that entities in a hierarchy should be handled by their specific repository or by the next specified ancestor's repository.
This is a demo project demonstrating the issue: demo.zip