Honor standalone @Collation on entities and propagate to repository base methods#5218
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
…ase methods. Read the standalone @collation annotation on the entity type from BasicMongoPersistentEntity. @Document.collation() keeps working through @aliasfor merging; standalone @collation now contributes the entity collation even when @document is absent or omits collation. Propagate the entity collation through SimpleMongoRepository and SimpleReactiveMongoRepository base CRUD methods (findById, existsById, findAll, count, deleteById, deleteAll, ...) so the collation is honored consistently, matching the behavior of Example-based methods. Closes: spring-projects#4535
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.
What this addresses
@Collationon an entity is not picked up unless it is set through@Document(collation = ...), and even when it is picked up, the entity collation only reachesExample-based repository methods. Plain CRUD methods (findById,findAll,count,deleteAll, …) are dispatched without it, so the resulting MongoDB operation runs with no collation even though the entity declares one.Closes: #4535
Root cause
Two cooperating gaps in the entity → repository wiring:
BasicMongoPersistentEntityonly consults@Document.collation(). The standalone@Collationannotation inorg.springframework.data.mongodb.core.annotationis the canonical source (it is what@Document.collation()is itself@AliasForfor), yet it is ignored unless@Documentis present and uses thecollationattribute. So@Collation(\"en_US\")on a type — with or without an accompanying@Document— silently has no effect.SimpleMongoRepository/SimpleReactiveMongoRepositoryonly propagateentityInformation.getCollation()on theExample-based query path. The basic CRUD methods (findById,existsById,findAll,findAll(Sort),findAll(Pageable),findAllById,count,deleteById,deleteAllById,deleteAll) build theirQuerywithout.collation(...), so even if (1) is fixed, the collation is dropped at the repository boundary.These are exactly the two locations called out in the issue thread.
Change summary
BasicMongoPersistentEntity— resolve collation viaAnnotatedElementUtils.findMergedAnnotation(rawType, Collation.class). Because@Documentis meta-annotated with@CollationandDocument#collationis declared as@AliasFor(annotation = Collation.class, attribute = \"value\"), the merged lookup returns the same value whether the user writes@Document(collation = \"en_US\")or@Collation(\"en_US\")(with or without@Document). Existing@Document.collation()behavior — literal value, SpEL expression, JSON document form — is preserved.SimpleMongoRepositoryandSimpleReactiveMongoRepository— add a smallapplyDefaultCollation(Query)helper and invoke it from every base CRUD method that builds aQuery(and from the sharedfindAll(Query)/getIdQuery(Iterable)helpers that funnelfindAllandfindAllById). The helper only sets the collation when one is defined and the query does not already carry one, so an explicitQuery.collation(...)from a caller still wins.Why this is the right fix
The maintainer (christophstrobl) diagnosed exactly this shape on the issue: "
BasicPersistentEntitydid not properly consider@Collationbut was only looking for@Documentand the collation was not properly passed on inSimpleMongoRepository." The change is additive — annotation handling extends to a strictly larger set of users (the existing@Document(collation=...)users keep getting the sameCollationinstance), and collation propagation aligns the base CRUD methods with theExample-based methods that have always carried it. Behavior for users that do not declare an entity collation is unchanged:entityInformation.hasCollation()returnsfalseandapplyDefaultCollationis a no-op.Tests
New tests added:
BasicMongoPersistentEntityUnitTests:readsStandaloneCollationAnnotation—@Document+ standalone@Collation(\"en_US\")resolves toCollation.of(\"en_US\").readsStandaloneCollationAnnotationWithoutDocumentAnnotation—@Collation(\"en_US\")without@Documentresolves correctly.SimpleMongoRepositoryUnitTests:shouldAddDefaultCollationToFindMethods(parameterized overfindAll(),findAll(Sort),findAll(Pageable)).shouldAddDefaultCollationToFindById,shouldAddDefaultCollationToExistsById,shouldAddDefaultCollationToCount,shouldAddDefaultCollationToDeleteById,shouldAddDefaultCollationToDeleteAll.SimpleReactiveMongoRepositoryUnitTests:shouldAddDefaultCollationToFindById,shouldAddDefaultCollationToCount,shouldAddDefaultCollationToDeleteAll.Existing collation tests (
DATAMONGO-1854,DATAMONGO-2565) continue to pass; they cover@Document(collation = ...)literal/JSON/SpEL forms.Verification done
main(BasicMongoPersistentEntity ignores standalone@Collation; SimpleMongoRepository drops collation on base CRUD methods).gh pr listand PR search onspring-projects/spring-data-mongodbfor any in-flight PR referencing @Collation annotation on documents, repositories or queries not interpreted #4535 — none.mainforBasicMongoPersistentEntity,SimpleMongoRepository,SimpleReactiveMongoRepository, andentityInformation.getCollation()to confirm the bug pattern is still present../mvnw -pl spring-data-mongodb test -Dtest='BasicMongoPersistentEntityUnitTests,SimpleMongoRepositoryUnitTests,SimpleReactiveMongoRepositoryUnitTests'→ 61 tests, all green../mvnw -pl spring-data-mongodb test -Dtest='BasicMongoPersistent*,*Mongo*Repository*UnitTests,*MongoQuery*UnitTests'→ 365 tests, all green.Scope notes
The issue also mentions
@Collationon the repository interface and on individual query methods. Those flow throughMongoQueryMethod(a separate code path fromBasicMongoPersistentEntity/SimpleMongoRepository) and were intentionally left out to keep this change focused on the entity-level case the maintainer explicitly confirmed; method-level@Collationalready works through the@Query(collation = ...)alias.