Fix for repositories inaccurately detected when multiple interfaces exist for domain type. - #2602
Open
ruthst00 wants to merge 2 commits into
Open
Fix for repositories inaccurately detected when multiple interfaces exist for domain type.#2602ruthst00 wants to merge 2 commits into
ruthst00 wants to merge 2 commits into
Conversation
…AREST-917): Repositories inaccurately detected when multiple interfaces exist for domain type. Signed-off-by: ruthes00 <ruthes00@gmail.com>
Three improvements on top of the original DATAREST-917-ruthes00 fix: 1. Fix getSearchResourceMappings() to use the exported repository The original fix correctly selected the exported (annotated) repository for the domain-type metadata cache slot, but getSearchResourceMappings() still fell back to repositories.getRequiredRepositoryInformation(domainType), which returns whichever repository Spring registered first (non-deterministic). When that happens to be the non-annotated repository, the annotated repository's query methods are invisible and /search returns 404. Fix: introduce a repositoryInfoByDomainType map that tracks the winning RepositoryInformation per domain type (updated in sync with the metadata cache). getSearchResourceMappings() now reads from this map first, falling back to Repositories only when no entry exists (backward-compatible path). 2. Correct @SInCE version tag (3.7 -> 5.0) The new ListableBeanFactory constructor was tagged @SInCE 3.7, but the current version line is 5.x. Updated to @SInCE 5.0. 3. Add JPA integration test (MultipleRepositoriesSameDomainTypeIntegrationTests) Full end-to-end test with two JPA repositories for the same Widget entity: - InternalWidgetRepository: package-protected, no annotation (not exported) - PublicWidgetRepository: public, @RepositoryRestResource (exported) Verifies: (1) GET /widgets returns 200 (annotated repo is exposed) (2) GET /widgets/search returns 200 and lists findByName (search fix) (3) POST/GET round-trip returns correct entity data Also adds a unit test (getSearchResourceMappingsUsesExportedRepositoryQueryMethods) to RepositoryResourceMappingsUnitTests covering the search-mappings gap. 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.
Fixes #1169 and #465
Root Cause
RepositoryResourceMappings.populateCache()iterated overPersistentEntities(one per domain type) and calledrepositories.getRequiredRepositoryInformation(type), which returns only oneRepositoryInformationper domain type. When multiple repository interfaces exist for the same domain type (e.g., a plainCrudRepositoryfor internal use and a@RepositoryRestResource-annotated one for REST exposure), only one was evaluated byRepositoryDetectionStrategy. If the annotated one wasn't the one returned, the domain type was never properly exported.Changes Made
RepositoryResourceMappings.java— Added a new constructor accepting aListableBeanFactory. When provided,populateCache()now iterates over allRepositoryFactoryInformationbeans viaBeanFactoryUtils.beansOfTypeIncludingAncestors, discovering every repository interface regardless of how many exist per domain type. The domain-type cache selection logic was also enhanced to prefer exported repositories over non-exported ones (in addition to the existing@Primarypreference). The original constructor is preserved as a fallback for backward compatibility.RepositoryRestMvcConfiguration.java— Updated theresourceMappings()@Beanmethod to use the new constructor, passingapplicationContextas theListableBeanFactory, so the fix is active in all Spring Data REST applications.RepositoryResourceMappingsUnitTests.java— Added two new unit tests that directly reproduce the bug scenario: one verifying that an@RepositoryRestResource-annotated repository is correctly detected even when a non-annotated repository is registered first, and one verifying that non-export behavior is preserved when only a package-protected repository exists.