Polishing.
Initialize MongoId also for reactive insertAll(…). See: #4944 Original pull request: #4945
This commit is contained in:
@@ -1433,11 +1433,16 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
entity.assertUpdateableIdIfNotSet();
|
||||
|
||||
T initialized = entity.initializeVersionProperty();
|
||||
Document dbDoc = entity.toMappedDocument(writer).getDocument();
|
||||
MappedDocument mapped = entity.toMappedDocument(writer);
|
||||
|
||||
maybeEmitEvent(new BeforeSaveEvent<>(initialized, dbDoc, collectionName));
|
||||
maybeEmitEvent(new BeforeSaveEvent<>(initialized, mapped.getDocument(), collectionName));
|
||||
return maybeCallBeforeSave(initialized, mapped.getDocument(), collectionName).map(toSave -> {
|
||||
|
||||
return maybeCallBeforeSave(initialized, dbDoc, collectionName).thenReturn(Tuples.of(entity, dbDoc));
|
||||
MappedDocument mappedDocument = queryOperations.createInsertContext(mapped)
|
||||
.prepareId(uninitialized.getClass());
|
||||
|
||||
return Tuples.of(entity, mappedDocument.getDocument());
|
||||
});
|
||||
});
|
||||
}).collectList();
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
@@ -84,6 +85,7 @@ import org.springframework.data.mongodb.test.util.ReactiveMongoTestTemplate;
|
||||
|
||||
import com.mongodb.WriteConcern;
|
||||
import com.mongodb.reactivestreams.client.MongoClient;
|
||||
import com.mongodb.reactivestreams.client.MongoCollection;
|
||||
|
||||
/**
|
||||
* Integration test for {@link MongoTemplate}.
|
||||
@@ -165,6 +167,19 @@ public class ReactiveMongoTemplateTests {
|
||||
assertThat(person.getId()).isNotNull();
|
||||
}
|
||||
|
||||
@Test // GH-4944
|
||||
void insertAllShouldConvertIdToTargetTypeBeforeSave() {
|
||||
|
||||
RawStringId walter = new RawStringId();
|
||||
walter.value = "walter";
|
||||
|
||||
RawStringId returned = template.insertAll(List.of(walter)).blockLast();
|
||||
template.execute(RawStringId.class, MongoCollection::find) //
|
||||
.as(StepVerifier::create) //
|
||||
.consumeNextWith(actual -> assertThat(returned.id).isEqualTo(actual.get("_id"))) //
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-1444
|
||||
void saveSetsId() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user