[CDRIVER-1335] libbson does not properly serialize the Code type to JSON Created: 16/Jun/16  Updated: 27/Dec/16  Resolved: 11/Nov/16

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

Type: Bug Priority: Major - P3
Reporter: Shane Harvey Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: intern2016
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CDRIVER-1364 BSON type "JavaScript code w/ scope" ... Closed
duplicates CDRIVER-1878 bson_append_code_with_scope drops emp... Closed
Related
related to CDRIVER-1695 libbson's bson-to-json does't allow f... Closed
related to PHPC-459 BSON objects should implement JsonSer... Closed
related to CDRIVER-1879 libbson truncates code_with_scope wit... Closed
related to PHPC-714 Implement BSON corpus test suite Closed
related to CDRIVER-1913 Parse "$code" extended JSON Closed
is related to CDRIVER-1364 BSON type "JavaScript code w/ scope" ... Closed
is related to DOCS-8119 Document "Code" BSON type's extended ... Closed
Backwards Compatibility: Minor Change

 Description   

The libsson function bson_as_json does not properly serialize the extended JSON type Code.

The expected behavior is to output an object with "$code" and "$scope" attributes as demonstrated by json_util from PyMongo

>>> import bson
>>> from bson import json_util
>>> from bson.code import Code
>>> json_util.dumps({"code": Code("function x() { return 1; }")})
'{"code": {"$code": "function x() { return 1; }", "$scope": {}}}'
>>> b = bson.BSON.encode({"code": Code("function x() { return 1; }")})
>>> b
'*\x00\x00\x00\rcode\x00\x1b\x00\x00\x00function x() { return 1; }\x00\x00'
>>> import bjson
>>> bjson.dumps(b)
'{ "code" : "function x() { return 1; }" }'
>>>

bjson.dumps is a thin wrapper around bson_as_json using the Python C API.



 Comments   
Comment by Githook User [ 28/Nov/16 ]

Author:

{u'username': u'jmikola', u'name': u'Jeremy Mikola', u'email': u'jmikola@gmail.com'}

Message: PHPC-459: Update Javascript::jsonSerialize() tests for CDRIVER-1335

The expected output of bson-javascript-jsonserialize-004.phpt is logically incorrect because of an outstanding bug where Javascript objects are always serialized to BSON code types, irrespective of their scope property. That issue will be fixed by PHPC-838.
Branch: master
https://github.com/mongodb/mongo-php-driver/commit/9eb4a80d2e4c406093ffb568fd1561a6687ad99d

Comment by Jeremy Mikola [ 14/Nov/16 ]

Right, the point of this ticket is we should include empty "$scope", if the BSON type is code with scope, so that the BSON type is properly roundtripped instead of silently changing to code without scope when it's converted to BSON again.

Sounds good. I incorrectly assumed this was going to always serialize a "$scope" field.

Comment by Githook User [ 11/Nov/16 ]

Author:

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

Message: CDRIVER-1335 bson_as_json for "code" types

bson_append_code_with_scope now preserves the "code with scope" type
if scope is an empty, non-NULL BSON document.

bson_as_json now properly formats BSON "code" and "code with scope"
types like '{"$code": "...", "$scope": {}}'. Before, it had treated the
code as a string and ignored the scope.
Branch: master
https://github.com/mongodb/libbson/commit/1048f4f78e306d513879ec0cb9fcc6e1be6a946b

Comment by A. Jesse Jiryu Davis [ 09/Nov/16 ]

Right, the point of this ticket is we should include empty "$scope", if the BSON type is code with scope, so that the BSON type is properly roundtripped instead of silently changing to code without scope when it's converted to BSON again.

Comment by Bernie Hackett [ 09/Nov/16 ]

I believe this is the format used by mongoexport. PyMongo was recently changed so that explicit empty scope is always output in extended JSON. That matches the behavior of mongoexport and the BSON corpus suite.

Comment by Jeremy Mikola [ 09/Nov/16 ]

jesse: In Slack, you mentioned:

that's a bug, we can fix it w/o worrying about BC, code with empty scope will be JSONified as shown:

`{"code": {"$code": "function x() { return 1; }", "$scope": {}}}'`

I assume this may be mimicking PyMongo, as Shane shared in the OP:

>>> json_util.dumps({"code": Code("function x() { return 1; }")})
'{"code": {"$code": "function x() { return 1; }", "$scope": {}}}'

Is it worth creating an empty "$scope" field when the Extended JSON spec supports both code and code-with-scope types? I'd think "$scope" would simply be omitted if the document is empty or not provided.

Comment by Bernie Hackett [ 17/Jun/16 ]

LOL. I opened DOCS-8113 about this yesterday.

Comment by A. Jesse Jiryu Davis [ 16/Jun/16 ]

Opened DOCS-8119.

Comment by A. Jesse Jiryu Davis [ 16/Jun/16 ]

Assigning to Ian Boros so he gets some exposure to libbson.

Comment by Hannes Magnusson [ 16/Jun/16 ]

Not documented on https://docs.mongodb.com/manual/reference/mongodb-extended-json/
But

{foo: { $code: ..., $scope: ...}}

is indeed what the server does.
https://github.com/mongodb/mongo/blob/master/src/mongo/dbtests/jsontests.cpp#L546-L559

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