Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 034a352

Browse files
christophstroblmp911de
authored andcommittedOct 12, 2022
Preserve given Id on insert.
This commit fixes an issue where an existing Id got replaced with a generated one when using MongoId annotation. Closes: #4184 Closes: #4197 Original pull request: #4203.
1 parent ff28789 commit 034a352

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed
 

‎spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryOperations.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ <T> MappedDocument prepareId(Class<T> type) {
272272
*/
273273
<T> MappedDocument prepareId(@Nullable MongoPersistentEntity<T> entity) {
274274

275-
if (entity == null) {
275+
if (entity == null || source.hasId()) {
276276
return source;
277277
}
278278

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -3636,6 +3636,26 @@ public void saveAndLoadStringThatIsAnObjectIdAsString() {
36363636
assertThat(target).isEqualTo(source);
36373637
}
36383638

3639+
@Test // GH-4184
3640+
void insertHonorsExistingRawId() {
3641+
3642+
RawStringId source = new RawStringId();
3643+
source.id = "abc";
3644+
source.value = "new value";
3645+
3646+
template.insert(source);
3647+
3648+
org.bson.Document result = template
3649+
.execute(db -> db.getCollection(template.getCollectionName(RawStringId.class))
3650+
.find().limit(1).cursor().next());
3651+
3652+
assertThat(result).isNotNull();
3653+
assertThat(result.get("_id")).isEqualTo("abc");
3654+
3655+
RawStringId target = template.findOne(query(where("id").is(source.id)), RawStringId.class);
3656+
assertThat(target).isEqualTo(source);
3657+
}
3658+
36393659
@Test // GH-4026
36403660
void saveShouldGenerateNewIdOfTypeIfExplicitlyDefined() {
36413661

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryOperationsUnitTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ void insertContextDoesNotAddConvertedIdForMongoIdTypesTargetingObjectId() {
205205
});
206206
}
207207

208+
@Test // GH-4184
209+
void insertContextDoesNotOverrideExistingId() {
210+
211+
assertThat(queryOperations.createInsertContext(new Document("_id", "abc")).prepareId(Person.class).getDocument())//
212+
.satisfies(result -> {
213+
assertThat(result).isEqualTo(new Document("_id", "abc"));
214+
});
215+
}
216+
208217
static class Person {
209218

210219
}

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,23 @@ void insertShouldGenerateNewIdOfTypeIfExplicitlyDefined() {
218218
}).verifyComplete();
219219
}
220220

221+
@Test // GH-4184
222+
void insertHonorsExistingRawId() {
223+
224+
MongoTemplateTests.RawStringId source = new MongoTemplateTests.RawStringId();
225+
source.id = "abc";
226+
source.value = "new value";
227+
228+
template.insert(source)
229+
.then(template.execute(db -> Flux.from(
230+
db.getCollection(template.getCollectionName(MongoTemplateTests.RawStringId.class)).find().limit(1).first()))
231+
.next())
232+
.as(StepVerifier::create).consumeNextWith(result -> {
233+
assertThat(result).isNotNull();
234+
assertThat(result.get("_id")).isEqualTo("abc");
235+
});
236+
}
237+
221238
@Test // DATAMONGO-1444
222239
void insertsSimpleEntityCorrectly() {
223240

0 commit comments

Comments
 (0)
Please sign in to comment.