DATAREST-846-ruthes00. Fixed bug in DefaultSelfLinkProvider.entityIdentifierOrNull() preventing use of composite key in resource URI - #2593
Open
ruthst00 wants to merge 1 commit into
Conversation
…entifierOrNull()` preventing use of composite key in resource URI. Signed-off-by: ruthes00 <ruthes00@gmail.com>
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.
Root Cause
When Spring Data REST serializes a collection of entities with
@EmbeddedIdcomposite keys, it callsPersistentEntity.getIdentifierAccessor(instance).getIdentifier()to extract the ID for building the self link. For@EmbeddedIdentities in Spring Data JPA, this can returnnullwhen the JPA mapping layer cannot resolve the composite key through its standard path (e.g., when the key type contains non-primitive fields like Joda-TimeDateTimethat aren't registered in the mapping context). Whennullis returned, the self link generation fails silently and falls back to the collection root URI — which is exactly the broken behavior reported.As described in DATAREST-846, the user's
BackendIdConverterwas correctly wired and would have been called (it works for direct item lookups), but it's never reached because the identifier extraction step returnsnullfirst.The Fix —
DefaultSelfLinkProvider.javaIn
entityIdentifierOrNull(), after the standardIdentifierAccessorpath returnsnull, fall back to reading the ID property directly viaPersistentPropertyAccessor:This returns the raw
SiteStatIdcomposite key object, which is then passed toRepositoryEntityLinks.linkForItemResource(), which calls the user'sBackendIdConverter.toRequestId()to produce the correct URI string (e.g.,1_2016-06-26T12:20:51.954Z). The self links in the collection response will then correctly point to/api/siteStats/1_2016-06-26T12:20:51.954Zinstead of the collection root.A new test
fallsBackToIdPropertyAccessorWhenIdentifierAccessorReturnsNullForCompositeKeywas added toDefaultSelfLinkProviderUnitTeststo cover this scenario. All 7 tests pass.