Fix issue where links to subclasses of abstract class (aggregate root) are not rendered correctly - #2609
Open
ruthst00 wants to merge 2 commits into
Open
Fix issue where links to subclasses of abstract class (aggregate root) are not rendered correctly#2609ruthst00 wants to merge 2 commits into
ruthst00 wants to merge 2 commits into
Conversation
Signed-off-by: ruthes00 <ruthes00@gmail.com>
…e repositories When a Spring Data REST repository is typed to an abstract JPA entity (e.g. MealRepository extends JpaRepository<Meal, Long>) and the actual instances returned at runtime are concrete subclasses (e.g. Dinner extends Meal), DefaultSelfLinkProvider was generating incorrect self-links pointing to a non-existent subclass resource path (e.g. /dinner/1) instead of the correct base-class resource path (e.g. /meals/1). Root cause: DefaultSelfLinkProvider.createSelfLinkFor() called entityLinks.linkToItemResource(instance.getClass(), id) using the concrete runtime type (Dinner) directly. Since no repository is registered for Dinner, EntityLinks fell back to generating a path from the class name, producing /dinner/1. Fix: Introduce a resolveRepositoryType() method in DefaultSelfLinkProvider that walks up the superclass hierarchy from the concrete type to find the nearest ancestor that is the exact domain type of a registered repository. The check uses Repositories.getRepositoryInformationFor(type) and verifies that the declared domain type matches exactly, distinguishing Meal (the repository domain type) from Dinner (a subclass that Repositories.hasRepositoryFor() also returns true for via hierarchy lookup). To support this, DefaultSelfLinkProvider gains an optional Repositories constructor parameter, and RepositoryRestMvcConfiguration.selfLinkProvider() is updated to inject the Repositories bean. Resolves: spring-projectsGH-1445 Signed-off-by: Steve Rutherford <ruthes00@example.com> Signed-off-by: ruthes00 <ruthes00@gmail.com>
ruthst00
force-pushed
the
issue/1445
branch
from
September 13, 2026 19:36
e4d82d2 to
372777c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1445
The Bug: When a Spring Data REST repository is typed to an abstract JPA entity (e.g.
MealRepository extends JpaRepository<Meal, Long>) and the actual instances returned at runtime are concrete subclasses (e.g.Dinner extends Meal),DefaultSelfLinkProviderwas generating incorrect self-links pointing to a non-existent subclass resource path (e.g./dinner/1) instead of the correct base-class resource path (e.g./meals/1). This made the self-links unresolvable (404).Root Cause:
DefaultSelfLinkProvider.createSelfLinkFor()calledentityLinks.linkToItemResource(instance.getClass(), id)using the concrete runtime type (Dinner) directly. Since no repository is registered forDinner,EntityLinksfell back to generating a path from the class name.The Fix (commit
e4d82d24):DefaultSelfLinkProvider— Added aresolveRepositoryType()method that walks up the superclass hierarchy from the concrete type to find the nearest ancestor that is the exact domain type of a registered repository. The check usesRepositories.getRepositoryInformationFor(type)and verifies the declared domain type matches exactly (distinguishingMealfromDinner, sinceRepositories.hasRepositoryFor()returnstruefor both via hierarchy lookup). Added an optionalRepositoriesconstructor parameter.RepositoryRestMvcConfiguration— UpdatedselfLinkProvider()bean method to inject theRepositoriesbean.MealRepositoryandDataRest1080Testswith 4 integration tests covering: correct self-link path, resolvable self-link, subclass field serialization, and collection resource self-links.All 4 new tests pass; the 2 pre-existing test failures (
RepositoryControllerIntegrationTests,PersistentEntitySerializationTests) were confirmed to exist before this fix and are unrelated.