Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data.build</groupId>
<artifactId>spring-data-parent</artifactId>
<version>4.0.0</version>
<version>4.1.0-SNAPSHOT</version>
</parent>

<modules>
Expand All @@ -26,7 +26,7 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>4.0.0</springdata.commons>
<springdata.commons>4.1.0-SNAPSHOT</springdata.commons>
<mongo>5.6.1</mongo>
<jmh.version>1.19</jmh.version>
</properties>
Expand Down Expand Up @@ -157,8 +157,20 @@


<repositories>


<repository>
<id>spring-snapshot</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>spring-milestone</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
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;
Expand All @@ -37,6 +40,11 @@
*/
public abstract class SerializationUtils {

private static final JsonWriterSettings LOGGING_JSON_SETTINGS =
JsonWriterSettings.builder()
.outputMode(JsonMode.SHELL)
.build();

private SerializationUtils() {

}
Expand Down Expand Up @@ -118,7 +126,14 @@ private static void toFlatMap(String currentPath, Object source, Map<String, Obj
}

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) {

Expand All @@ -140,7 +155,11 @@ public static String serializeValue(@Nullable Object value) {
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;

Expand Down Expand Up @@ -117,6 +118,25 @@ public void flattenMapShouldReturnEmptyMapWhenSourceIsNull() {
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 {

}
Expand Down