Skip to content

GH-5166 - Apply BeforeConvertCallback result in MongoTemplate.replace()#5211

Open
SUJAY-HK wants to merge 1 commit into
spring-projects:mainfrom
SUJAY-HK:gh-5166-before-convert-callback-on-replace
Open

GH-5166 - Apply BeforeConvertCallback result in MongoTemplate.replace()#5211
SUJAY-HK wants to merge 1 commit into
spring-projects:mainfrom
SUJAY-HK:gh-5166-before-convert-callback-on-replace

Conversation

@SUJAY-HK

@SUJAY-HK SUJAY-HK commented Jun 14, 2026

Copy link
Copy Markdown
  • 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).

What

MongoTemplate.replace() and ReactiveMongoTemplate.replace() call
maybeCallBeforeConvert but the returned (potentially modified) entity
is not used for BSON mapping. replaceSingleContext was already called
with the original entity before the callback fires, so the callback
result is silently discarded.

Why

The BeforeConvertCallback contract requires that the entity returned
by the callback is the one that gets mapped and persisted. The same
class of bug was previously fixed for doInsert() in #2481.

Fix

Move maybeCallBeforeConvert before replaceSingleContext in
MongoTemplate.replace() so the post-callback entity is used for
document mapping.

In ReactiveMongoTemplate.replace(), wrap the context creation and
collection operation inside .flatMap() on the callback result, since
maybeCallBeforeConvert returns Mono<T> in the reactive stack.

Tests added in MongoTemplateTests and ReactiveMongoTemplateTests
to verify the callback result is applied before the replacement is
persisted. Full test suite passes (6303 tests, 0 failures).

Closes #5166

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 14, 2026
…emplate.replace()

Signed-off-by: SUJAY <hksujay03@gmail.com>
@SUJAY-HK SUJAY-HK force-pushed the gh-5166-before-convert-callback-on-replace branch from 27af3c7 to 3ee0e1e Compare June 14, 2026 14:24
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BeforeConvertCallback not applied on 'replace'

4 participants