spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
923 stars 562 forks source link

Inconsistent handling of polymorphic entities with only a repository for the root entity type #2170

Open alienisty opened 2 years ago

alienisty commented 2 years ago

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:

@Entity
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = STRING)
public abstract class Address {
  ...
}

@Entity
@DiscriminatorValue("Residential")
public class ResidentialAddress extends Address {
}

@Entity
@DiscriminatorValue("Business")
public class BusinessAddress extends Address {
}

@Entity
public class Person {
  ...
  @OneToOne(fetch = FetchType.LAZY)
  private ResidentialAddress residentialAddress;
  ...
}

@RepositoryRestResource(path = "addresses", collectionResourceRel = "addresses")
public interface AddressRepository extends JpaRepository<Address, Long> {
}

@RepositoryRestResource(path = "people", collectionResourceRel = "people")
public interface PersonRepository extends JpaRepository<Person, Long> {
}

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':

{
  "_embedded" : {
    "addresses" : [ {
      "label" : "Home",
      "address" : "The Elves",
      "_links" : {
        "self" : {
          "href" : "http://localhost/addresses/1"
        },
        "address" : {
          "href" : "http://localhost/addresses/1"
        }
      }
    } ],
    "businessAddresses" : [ {
      "label" : "Holiday",
      "address" : null,
      "_links" : {
        "self" : {
          "href" : "http://localhost/businessAddress/2"
        },
        "businessAddress" : {
          "href" : "http://localhost/businessAddress/2"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost/addresses"
    },
    "profile" : {
      "href" : "http://localhost/profile/addresses"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

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

alienisty commented 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
  }
}