Skip to content

Fix for repositories inaccurately detected when multiple interfaces exist for domain type. - #2602

Open
ruthst00 wants to merge 2 commits into
spring-projects:mainfrom
ruthst00:DATAREST-917-ruthes00
Open

Fix for repositories inaccurately detected when multiple interfaces exist for domain type.#2602
ruthst00 wants to merge 2 commits into
spring-projects:mainfrom
ruthst00:DATAREST-917-ruthes00

Conversation

@ruthst00

@ruthst00 ruthst00 commented Sep 10, 2026

Copy link
Copy Markdown

Fixes #1169 and #465

Root Cause

RepositoryResourceMappings.populateCache() iterated over PersistentEntities (one per domain type) and called repositories.getRequiredRepositoryInformation(type), which returns only one RepositoryInformation per domain type. When multiple repository interfaces exist for the same domain type (e.g., a plain CrudRepository for internal use and a @RepositoryRestResource-annotated one for REST exposure), only one was evaluated by RepositoryDetectionStrategy. 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 a ListableBeanFactory. When provided, populateCache() now iterates over all RepositoryFactoryInformation beans via BeanFactoryUtils.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 @Primary preference). The original constructor is preserved as a fallback for backward compatibility.

RepositoryRestMvcConfiguration.java — Updated the resourceMappings() @Bean method to use the new constructor, passing applicationContext as the ListableBeanFactory, 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.


  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

…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>
@ruthst00 ruthst00 changed the title DATAREST-917-ruthes00: Fix for repositories inaccurately detected when multiple interfaces exist for domain type. Fix for repositories inaccurately detected when multiple interfaces exist for domain type. Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repositories inaccurately detected when multiple interfaces exist for domain type [DATAREST-917]

2 participants