spring-projects / spring-projects/spring-data-mongodb
BeforeConvertCallback not applied on 'replace'
Open
@christophstrobl is already working on this.
Since Feb 19, 2026.
status: waiting-for-triage
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
When using mongoTemplate.replace(query, entity) to replace a document in the database, the returned value from BeforeConvertCallbacks is not used as the entity to save.
It seems that the callbacks are actually called, but the resulting replacementis not used to create the mappedReplacement, which then gets saved. From Commit 909169d, class org.springframework.data.mongodb.core.MongoTemplate, lines 2153-2186:
protected <S, T> UpdateResult replace(Query query, Class<S> entityType, T replacement, ReplaceOptions options,
String collectionName) {
...
UpdateContext updateContext = queryOperations.replaceSingleContext(query,
operations.forEntity(replacement).toMappedDocument(this.mongoConverter), options.isUpsert());
replacement = maybeCallBeforeConvert(replacement, collectionName);
Document mappedReplacement = updateContext.getMappedUpdate(mappingContext.getPersistentEntity(entityType));
maybeEmitEvent(new BeforeSaveEvent<>(replacement, mappedReplacement, collectionName));
replacement = maybeCallBeforeSave(replacement, mappedReplacement, collectionName);
MongoAction action = new MongoAction(writeConcern, MongoActionOperation.REPLACE, collectionName, entityType,
mappedReplacement, updateContext.getQueryObject());
UpdateResult result = doReplace(options, entityType, collectionName, updateContext,
createCollectionPreparer(query, action), mappedReplacement);
...
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.