From 0b604ed8929b5fccb251627911c091c6b61091e7 Mon Sep 17 00:00:00 2001 From: Benedikt Waldvogel Date: Tue, 19 Mar 2024 12:52:22 +0100 Subject: [PATCH] Fix replace with empty document (#227) (#229) --- .../mongo/backend/AbstractMongoCollection.java | 6 +++--- .../bwaldvogel/mongo/backend/AbstractBackendTest.java | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java index 3104b002..c9f8709d 100755 --- a/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java +++ b/core/src/main/java/de/bwaldvogel/mongo/backend/AbstractMongoCollection.java @@ -345,14 +345,14 @@ private Document calculateUpdateDocument(Document oldDocument, Document update, newDocument.put(getIdField(), oldDocument.get(getIdField())); } - if (numStartsWithDollar == update.keySet().size()) { + if (numStartsWithDollar == 0) { + applyUpdate(newDocument, update); + } else if (numStartsWithDollar == update.keySet().size()) { validateUpdateQuery(update); oldDocument.cloneInto(newDocument); for (String key : update.keySet()) { modifyField(newDocument, key, update, arrayFilters, matchPos, isUpsert); } - } else if (numStartsWithDollar == 0) { - applyUpdate(newDocument, update); } else { throw new MongoServerException("illegal update: " + update); } diff --git a/test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractBackendTest.java b/test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractBackendTest.java index 8170355c..8b95e174 100755 --- a/test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractBackendTest.java +++ b/test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractBackendTest.java @@ -3689,6 +3689,17 @@ void testReplaceOneUpsertsWithGeneratedId() throws Exception { .isInstanceOf(ObjectId.class); } + // https://github.com/bwaldvogel/mongo-java-server/issues/227 + @Test + void testReplaceWithEmptyDocument() throws Exception { + collection.insertOne(json("_id: 'myId', value: 'test'")); + + collection.replaceOne(json("_id: 'myId'"), json("")); + + assertThat(collection.find()) + .containsExactly(json("_id: 'myId'")); + } + // https://github.com/bwaldvogel/mongo-java-server/issues/41 @Test void testBulkUpsert() throws Exception {