Browse Source

Merge 9776b35c9b into c54ceb6415

pull/5109/merge
Mohammadali Jalalkamali 2 weeks ago committed by GitHub
parent
commit
94ff0582de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 23
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/SerializationUtils.java
  2. 20
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SerializationUtilsUnitTests.java

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

@ -23,6 +23,9 @@ import java.util.LinkedHashMap; @@ -23,6 +23,9 @@ import java.util.LinkedHashMap;
import java.util.Map;
import org.bson.Document;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;
import org.bson.types.ObjectId;
import org.jspecify.annotations.Nullable;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Contract;
@ -37,6 +40,11 @@ import org.springframework.util.ObjectUtils; @@ -37,6 +40,11 @@ import org.springframework.util.ObjectUtils;
*/
public abstract class SerializationUtils {
private static final JsonWriterSettings LOGGING_JSON_SETTINGS =
JsonWriterSettings.builder()
.outputMode(JsonMode.SHELL)
.build();
private SerializationUtils() {
}
@ -118,7 +126,14 @@ public abstract class SerializationUtils { @@ -118,7 +126,14 @@ public abstract class SerializationUtils {
}
try {
String json = value instanceof Document document ? document.toJson() : serializeValue(value);
String json;
if (value instanceof Document document) {
json = document.toJson(LOGGING_JSON_SETTINGS);
} else {
json = serializeValue(value);
}
return json.replaceAll("\":", "\" :").replaceAll("\\{\"", "{ \"");
} catch (Exception e) {
@ -140,7 +155,11 @@ public abstract class SerializationUtils { @@ -140,7 +155,11 @@ public abstract class SerializationUtils {
return "null";
}
String documentJson = new Document("toBeEncoded", value).toJson();
if (value instanceof ObjectId objectId) {
return "ObjectId(\"" + objectId.toHexString() + "\")";
}
String documentJson = new Document("toBeEncoded", value).toJson(LOGGING_JSON_SETTINGS);
return documentJson.substring(documentJson.indexOf(':') + 1, documentJson.length() - 1).trim();
}

20
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/SerializationUtilsUnitTests.java

@ -22,6 +22,7 @@ import java.util.Arrays; @@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.Map;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.query.SerializationUtils;
@ -117,6 +118,25 @@ public class SerializationUtilsUnitTests { @@ -117,6 +118,25 @@ public class SerializationUtilsUnitTests {
assertThat(flattenMap(null)).isEmpty();
}
@Test
void shouldRenderStandaloneObjectIdInShellFormat() {
ObjectId id = new ObjectId("507f1f77bcf86cd799439011");
String result = SerializationUtils.serializeValue(id);
assertThat(result).isEqualTo("ObjectId(\"507f1f77bcf86cd799439011\")");
}
@Test
void shouldRenderDocumentWithObjectIdInShellFormat() {
ObjectId id = new ObjectId("507f1f77bcf86cd799439011");
Document doc = new Document("_id", id);
String result = SerializationUtils.serializeToJsonSafely(doc);
assertThat(result)
.contains("ObjectId(\"507f1f77bcf86cd799439011\")")
.doesNotContain("\"$oid\"");
}
static class Complex {
}

Loading…
Cancel
Save