Skip to content

Commit 07f7247

Browse files
committed
DATAMONGO-1002 - Update.toString() now uses SerializationUtils.
A simple call of toString() on a DBObject might result in an exception if the DBObject contains objects that are non-native MongoDB types (i.e. types that need to be converted prior to persistence). We now use SerializationUtils.serializeToJsonSafely(…) to avoid exceptions.
1 parent f669711 commit 07f7247

File tree

2 files changed

+12
-1
lines changed
  • spring-data-mongodb/src

2 files changed

+12
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public boolean equals(Object obj) {
400400
*/
401401
@Override
402402
public String toString() {
403-
return getUpdateObject().toString();
403+
return SerializationUtils.serializeToJsonSafely(getUpdateObject());
404404
}
405405

406406
/**

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collections;
2222
import java.util.Map;
2323

24+
import org.joda.time.DateTime;
2425
import org.junit.Test;
2526

2627
import com.mongodb.BasicDBObject;
@@ -409,4 +410,14 @@ public void getUpdateObjectShouldReturnCurrentDateCorrectlyWhenUsingMixedDateAnd
409410
equalTo(new BasicDBObjectBuilder().add("$currentDate",
410411
new BasicDBObject("foo", true).append("bar", new BasicDBObject("$type", "timestamp"))).get()));
411412
}
413+
414+
/**
415+
* @see DATAMONGO-1002
416+
*/
417+
@Test
418+
public void toStringWorksForUpdateWithComplexObject() {
419+
420+
Update update = new Update().addToSet("key", new DateTime());
421+
assertThat(update.toString(), is(notNullValue()));
422+
}
412423
}

0 commit comments

Comments
 (0)