Skip to content

BeforeConvertCallback not applied on 'replace' #5166

@NikiElich4235

Description

@NikiElich4235

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);
    ...
    }

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions