[SERVER-63709] ValueWriter cannot serialize objects that have properties with type Symbol Created: 16/Feb/22  Updated: 01/Feb/23  Resolved: 01/Feb/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Justin Seyster Assignee: Projjal Chanda
Resolution: Won't Fix Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-61234 JS object-based types are not properl... Closed
Assigned Teams:
Query Execution
Operating System: ALL
Participants:

 Description   

MongoDB Enterprise > db.symbol.insert({_id: 1, sym: Symbol("foo")})
Error: unable to convert JavaScript property to mongo element sym :
addToOperationsList@src/mongo/shell/bulk_api.js:606:28
Bulk/this.insert@src/mongo/shell/bulk_api.js:649:20
DBCollection.prototype.insert@src/mongo/shell/collection.js:317:9
@(shell):1:1

The shell wants to use ValueWriter to convert the JSON document (with its Symbol property) to BSON, but ValueWriter is missing a case for the Symbol object, which should be translated to a BSONType::Symbol.

https://github.com/mongodb/mongo/blob/581c58c475a872e25b2e3bf7cf5ccd52425ef7c7/src/mongo/scripting/mozjs/valuewriter.cpp#L248-L278



 Comments   
Comment by Projjal Chanda [ 01/Feb/23 ]

Closing this as won't fix since bson symbols are deprecated.

Comment by Justin Seyster [ 16/Feb/22 ]

A preliminary solution would look like this:

diff --git a/src/mongo/scripting/mozjs/valuewriter.cpp b/src/mongo/scripting/mozjs/valuewriter.cpp
index db483a36564..fbef592fb1c 100644
--- a/src/mongo/scripting/mozjs/valuewriter.cpp
+++ b/src/mongo/scripting/mozjs/valuewriter.cpp
@@ -326,6 +326,16 @@ void ValueWriter::writeThis(BSONObjBuilder* b,
     if (_value.isString()) {
         JSStringWrapper jsstr;
         b->append(sd, toStringData(&jsstr));
+    } else if(_value.isSymbol()) {
+        // Based on js::SymbolDescriptiveString()
+        JS::RootedSymbol symbol(_context, _value.toSymbol());
+        JS::RootedString symbolStr(_context, GetSymbolDescription(symbol));
+        if (symbolStr) {
+            JSStringWrapper jsstr(_context, symbolStr);
+            b->appendSymbol(sd, jsstr.toStringData());
+        } else {
+            b->appendSymbol(sd, StringData());
+        }
     } else if (_value.isNumber()) {
         double val = toNumber();

Generated at Thu Feb 08 05:58:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.