[CDRIVER-1951] Incorrect quoting in "code" to json Created: 09/Dec/16  Updated: 27/Oct/23  Resolved: 07/Jan/17

Status: Closed
Project: C Driver
Component/s: json, libbson
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Hannes Magnusson Assignee: A. Jesse Jiryu Davis
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: libbson corpus

 Description   

 ./example-client 
{ "_id" : { "$oid" : "58211a3176d7e4da02ded8b3" }, "hello" : "world" }
{ "_id" : { "$oid" : "58211a53b9f65c412c1560bf" } }
{ "_id" : { "$oid" : "58211a7eb9f65c412c1577ed" } }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be2" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be3" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be4" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be5" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be6" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be7" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be8" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076be9" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076bea" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829e9f6b9f65c3988076beb" }, "code" : "var a = {};" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369222" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369223" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369224" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369225" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369226" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369227" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369228" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c369229" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c36922a" }, "code" : "function() { return \"a" }
{ "_id" : { "$oid" : "5829ea48b9f65c3b1c36922b" }, "code" : "function() { return \"a" }

that return \"a" looks like a bug



 Comments   
Comment by Hannes Magnusson [ 07/Jan/17 ]

OIC. I've no idea how I got that data into my database then.
I thought it was round tripped in the example.

Comment by A. Jesse Jiryu Davis [ 07/Jan/17 ]

Hannes I think you're confused. I showed, above, that if I insert a correctly quoted Code object into MongoDB, then I can use our own example-client.c to print it correctly quoted. Since you used example-client.c to print incorrectly quoted Code, I bet that it was incorrectly quoted when you originally inserted it.

Comment by Hannes Magnusson [ 07/Jan/17 ]

This is our own example that produces that output:
https://github.com/mongodb/mongo-c-driver/blob/master/examples/example-client.c

Comment by Githook User [ 07/Jan/17 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-1951 test escaped quotes in BSON Code as JSON
Branch: master
https://github.com/mongodb/libbson/commit/5ec0091b06e892ef2817bbb54fe6d409ac1b3dc1

Comment by A. Jesse Jiryu Davis [ 07/Jan/17 ]

I've tested libbson and it works correctly:

bson_t code = BSON_INITIALIZER;
char *str;
 
BSON_APPEND_CODE (&code, "c", "return \"a\"");
str = bson_as_json (&code, NULL);
printf ("%s\n", str);

This correctly outputs:

{ "c" : { "$code" : "return \"a\"" } }

To verify, you could compare with PyMongo:

>>> from bson.json_util import dumps
>>> print(dumps({'c': Code('return "a"')}))
{"c": {"$code": "return \"a\""}}

If I insert such a document with PyMongo, like this:

MongoClient().test.test.insert({'c': Code('return "a"')})

.... then running the C Driver's "example-client" outputs this, as expected:

{ "_id" : { "$oid" : "587052cda08bff173675684c" }, "c" : { "$code" : "return \"a\"" } }

I suspect you're incorrectly quoting your inputs before you store them in MongoDB.

Generated at Wed Feb 07 21:13:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.