PYTHON-4084 Fix BSON inflation for DBRef (#1458)

This commit is contained in:
Noah Stapp 2023-12-12 13:00:14 -08:00 committed by GitHub
parent 568a3b1294
commit 9d32a09e30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -1884,6 +1884,11 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
goto invalid;
}
if (options->is_raw_bson) {
*position += size;
break;
}
/* Hook for DBRefs */
value = _dbref_hook(self, value);
if (!value) {

View File

@ -22,7 +22,7 @@ sys.path[0:0] = [""]
from test import client_context, unittest
from test.test_client import IntegrationTest
from bson import Code, decode, encode
from bson import Code, DBRef, decode, encode
from bson.binary import JAVA_LEGACY, Binary, UuidRepresentation
from bson.codec_options import CodecOptions
from bson.errors import InvalidBSON
@ -205,6 +205,14 @@ class TestRawBSONDocument(IntegrationTest):
self.assertEqual(decode(encode(doc)), {"value": Code("x=1", {})})
self.assertEqual(doc["value"].scope, RawBSONDocument(encode({})))
def test_contains_dbref(self):
doc = RawBSONDocument(encode({"value": DBRef("test", "id")}))
raw = {"$ref": "test", "$id": "id"}
raw_encoded = encode(decode(encode(raw)))
self.assertEqual(decode(encode(doc)), {"value": DBRef("test", "id")})
self.assertEqual(doc["value"].raw, raw_encoded)
if __name__ == "__main__":
unittest.main()