Skip to content

Commit d1dc112

Browse files
committed
Recurse when dumping DBRefs PYTHON-433
1 parent 95fe4f0 commit d1dc112

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

bson/json_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def default(obj):
163163
if isinstance(obj, ObjectId):
164164
return {"$oid": str(obj)}
165165
if isinstance(obj, DBRef):
166-
return obj.as_doc()
166+
return _json_convert(obj.as_doc())
167167
if isinstance(obj, datetime.datetime):
168168
# TODO share this code w/ bson.py?
169169
if obj.utcoffset() is not None:

test/test_json_util.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,8 @@ def test_objectid(self):
6363
def test_dbref(self):
6464
self.round_trip({"ref": DBRef("foo", 5)})
6565
self.round_trip({"ref": DBRef("foo", 5, "db")})
66-
67-
# TODO this is broken when using cjson. See:
68-
# http://jira.mongodb.org/browse/PYTHON-153
69-
# http://bugs.python.org/issue6105
70-
#
71-
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\", \"$id\": 5}}",
72-
# json.dumps({"ref": DBRef("foo", 5)},
73-
# default=json_util.default))
74-
# self.assertEqual("{\"ref\": {\"$ref\": \"foo\",
75-
# \"$id\": 5, \"$db\": \"bar\"}}",
76-
# json.dumps({"ref": DBRef("foo", 5, "bar")},
77-
# default=json_util.default))
66+
self.round_trip({"ref": DBRef("foo", ObjectId())})
67+
self.round_trip({"ref": DBRef("foo", ObjectId(), "db")})
7868

7969
def test_datetime(self):
8070
# only millis, not micros
@@ -128,7 +118,9 @@ def test_cursor(self):
128118
{'foo': [1, 2]},
129119
{'bar': {'hello': 'world'}},
130120
{'code': Code("function x() { return 1; }")},
131-
{'bin': Binary(b("\x00\x01\x02\x03\x04"))}
121+
{'bin': Binary(b("\x00\x01\x02\x03\x04"))},
122+
{'dbref': {'_ref': DBRef('simple',
123+
ObjectId('509b8db456c02c5ab7e63c34'))}}
132124
]
133125

134126
db.test.insert(docs)

0 commit comments

Comments
 (0)