Skip to content

Commit 9776b35

Browse files
committed
GH-4873: Use JsonWriterSettings(SHELL) for logging
Signed-off-by: Mohammadali Jalalkamali <[email protected]>
1 parent 33e2e68 commit 9776b35

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import java.util.Map;
2424

2525
import org.bson.Document;
26+
import org.bson.json.JsonMode;
27+
import org.bson.json.JsonWriterSettings;
28+
import org.bson.types.ObjectId;
2629
import org.jspecify.annotations.Nullable;
2730
import org.springframework.core.convert.converter.Converter;
2831
import org.springframework.lang.Contract;
@@ -37,6 +40,11 @@
3740
*/
3841
public abstract class SerializationUtils {
3942

43+
private static final JsonWriterSettings LOGGING_JSON_SETTINGS =
44+
JsonWriterSettings.builder()
45+
.outputMode(JsonMode.SHELL)
46+
.build();
47+
4048
private SerializationUtils() {
4149

4250
}
@@ -118,7 +126,14 @@ private static void toFlatMap(String currentPath, Object source, Map<String, Obj
118126
}
119127

120128
try {
121-
String json = value instanceof Document document ? document.toJson() : serializeValue(value);
129+
String json;
130+
131+
if (value instanceof Document document) {
132+
json = document.toJson(LOGGING_JSON_SETTINGS);
133+
} else {
134+
json = serializeValue(value);
135+
}
136+
122137
return json.replaceAll("\":", "\" :").replaceAll("\\{\"", "{ \"");
123138
} catch (Exception e) {
124139

@@ -140,7 +155,11 @@ public static String serializeValue(@Nullable Object value) {
140155
return "null";
141156
}
142157

143-
String documentJson = new Document("toBeEncoded", value).toJson();
158+
if (value instanceof ObjectId objectId) {
159+
return "ObjectId(\"" + objectId.toHexString() + "\")";
160+
}
161+
162+
String documentJson = new Document("toBeEncoded", value).toJson(LOGGING_JSON_SETTINGS);
144163
return documentJson.substring(documentJson.indexOf(':') + 1, documentJson.length() - 1).trim();
145164
}
146165

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

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

2424
import org.bson.Document;
25+
import org.bson.types.ObjectId;
2526
import org.junit.jupiter.api.Test;
2627
import org.springframework.data.mongodb.core.query.SerializationUtils;
2728

@@ -117,6 +118,25 @@ public void flattenMapShouldReturnEmptyMapWhenSourceIsNull() {
117118
assertThat(flattenMap(null)).isEmpty();
118119
}
119120

121+
@Test
122+
void shouldRenderStandaloneObjectIdInShellFormat() {
123+
ObjectId id = new ObjectId("507f1f77bcf86cd799439011");
124+
String result = SerializationUtils.serializeValue(id);
125+
assertThat(result).isEqualTo("ObjectId(\"507f1f77bcf86cd799439011\")");
126+
}
127+
128+
@Test
129+
void shouldRenderDocumentWithObjectIdInShellFormat() {
130+
ObjectId id = new ObjectId("507f1f77bcf86cd799439011");
131+
Document doc = new Document("_id", id);
132+
133+
String result = SerializationUtils.serializeToJsonSafely(doc);
134+
135+
assertThat(result)
136+
.contains("ObjectId(\"507f1f77bcf86cd799439011\")")
137+
.doesNotContain("\"$oid\"");
138+
}
139+
120140
static class Complex {
121141

122142
}

0 commit comments

Comments
 (0)