diff --git a/bindings/python/pymongoarrow/lib.pyx b/bindings/python/pymongoarrow/lib.pyx index 58d45de1..be4b8b61 100644 --- a/bindings/python/pymongoarrow/lib.pyx +++ b/bindings/python/pymongoarrow/lib.pyx @@ -187,12 +187,12 @@ cdef class BuilderManager: raise ValueError("Could not append raw value to", full_key.decode('utf8')) # Recurse into documents. - if value_t == BSON_TYPE_DOCUMENT: + if value_t == BSON_TYPE_DOCUMENT and builder.type_marker == BSON_TYPE_DOCUMENT: bson_iter_recurse(doc_iter, &child_iter) self.parse_document(&child_iter, full_key, BSON_TYPE_DOCUMENT) # Recurse into arrays. - if value_t == BSON_TYPE_ARRAY: + if value_t == BSON_TYPE_ARRAY and builder.type_marker == BSON_TYPE_ARRAY: bson_iter_recurse(doc_iter, &child_iter) self.parse_document(&child_iter, full_key, BSON_TYPE_ARRAY) diff --git a/bindings/python/test/test_arrow.py b/bindings/python/test/test_arrow.py index 53bef0e2..bfb10ee1 100644 --- a/bindings/python/test/test_arrow.py +++ b/bindings/python/test/test_arrow.py @@ -492,6 +492,12 @@ def test_schema_missing_field(self): out = func(self.coll, {} if func == find_arrow_all else [], schema=schema).drop(["_id"]) self.assertEqual(out["list_field"].to_pylist(), expected) + def test_schema_incorrect_data_type(self): + self.coll.delete_many({}) + self.coll.insert_one({"x": {"y": 1}}) + out = find_arrow_all(self.coll, {}, schema=Schema({"x": str})) + assert out.to_pylist() == [{"x": None}] + def test_auto_schema_nested(self): # Create table with random data of various types. _, data = self._create_nested_data()