[CDRIVER-1366] libbson has different behavior when parsing a special type at the top level Created: 06/Jul/16  Updated: 27/Nov/16  Resolved: 27/Nov/16

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

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

Issue Links:
Related
is related to CDRIVER-1123 Defective JSON string lets API bson_j... Closed

 Description   

Libbson has different behavior while parsing a MongoDB Extended JSON type at the top level (without a key). The behavior depends on the presence of other keys.

In this example I'm using "$numberLong" but the same behavior is reproducible with other special types.

(python2.6) ➜  python-bsonjs git:(master) ✗ python
Python 2.6.9 (unknown, Oct 23 2015, 19:19:20)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bsonjs
>>> print(bsonjs.loads.__doc__)
load(json) -> bytes
 
Encode `json` (a `str` or `bytes-like object` containing a MongoDB Extended
JSON document) to BSON bytes.
This function wraps `bson_init_from_json` from libbson.
>>> bsonjs.loads('{"$numberLong": "1"}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid MongoDB extended JSON
>>> bsonjs.loads('{"$numberLong": "1", "a": 1}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid key a": 1}.  Looking for values for 18
>>> bsonjs.loads('{"a": 1, "$numberLong": "1"}')
'\x1f\x00\x00\x00\x10a\x00\x01\x00\x00\x00\x02$numberLong\x00\x02\x00\x00\x001\x00\x00'
>>> bsonjs.loads('{"a": 1, "$numberLong": "1", "a": 1}')
'&\x00\x00\x00\x10a\x00\x01\x00\x00\x00\x02$numberLong\x00\x02\x00\x00\x001\x00\x10a\x00\x01\x00\x00\x00\x00'



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

This was fixed as part of CDRIVER-1123 - the behavior is unchanged since the initial bug report, except that the weird error message is replaced with the proper "Invalid MongoDB extended JSON".

Comment by A. Jesse Jiryu Davis [ 07/Jul/16 ]

The bug is, the second error message is weird. Other than that I think this works as designed.

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