Skip to content

Commit 9a3b213

Browse files
authored
INTPYTHON-490 Fix segmentation fault when document doesn't match schema (#261)
1 parent 66c8f3f commit 9a3b213

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

bindings/python/pymongoarrow/lib.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ cdef class BuilderManager:
187187
raise ValueError("Could not append raw value to", full_key.decode('utf8'))
188188

189189
# Recurse into documents.
190-
if value_t == BSON_TYPE_DOCUMENT:
190+
if value_t == BSON_TYPE_DOCUMENT and builder.type_marker == BSON_TYPE_DOCUMENT:
191191
bson_iter_recurse(doc_iter, &child_iter)
192192
self.parse_document(&child_iter, full_key, BSON_TYPE_DOCUMENT)
193193

194194
# Recurse into arrays.
195-
if value_t == BSON_TYPE_ARRAY:
195+
if value_t == BSON_TYPE_ARRAY and builder.type_marker == BSON_TYPE_ARRAY:
196196
bson_iter_recurse(doc_iter, &child_iter)
197197
self.parse_document(&child_iter, full_key, BSON_TYPE_ARRAY)
198198

bindings/python/test/test_arrow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ def test_schema_missing_field(self):
492492
out = func(self.coll, {} if func == find_arrow_all else [], schema=schema).drop(["_id"])
493493
self.assertEqual(out["list_field"].to_pylist(), expected)
494494

495+
def test_schema_incorrect_data_type(self):
496+
self.coll.delete_many({})
497+
self.coll.insert_one({"x": {"y": 1}})
498+
out = find_arrow_all(self.coll, {}, schema=Schema({"x": str}))
499+
assert out.to_pylist() == [{"x": None}]
500+
495501
def test_auto_schema_nested(self):
496502
# Create table with random data of various types.
497503
_, data = self._create_nested_data()

0 commit comments

Comments
 (0)