Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bson/json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,9 @@ def _parse_canonical_minkey(doc: Any, dummy0: Any) -> MinKey:
def _parse_canonical_maxkey(doc: Any, dummy0: Any) -> MaxKey:
"""Decode a JSON MaxKey to bson.max_key.MaxKey."""
if type(doc["$maxKey"]) is not int or doc["$maxKey"] != 1:
raise TypeError("$maxKey value must be 1: %s", (doc,))
raise TypeError(f"$maxKey value must be 1: {doc}")
if len(doc) != 1:
raise TypeError(f"Bad $minKey, extra field(s): {doc}")
raise TypeError(f"Bad $maxKey, extra field(s): {doc}")
return MaxKey()


Expand Down
7 changes: 7 additions & 0 deletions test/test_json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ def test_minkey(self):
def test_maxkey(self):
self.round_trip({"m": MaxKey()})

def test_minkey_maxkey_errors(self):
for key in ("$minKey", "$maxKey"):
with self.assertRaisesRegex(TypeError, rf"^\{key} value must be 1: "):
json_util.loads(json.dumps({key: 2}))
with self.assertRaisesRegex(TypeError, rf"^Bad \{key}, extra field\(s\): "):
json_util.loads(json.dumps({key: 1, "x": 1}))

def test_timestamp(self):
dct = {"ts": Timestamp(4, 13)}
res = json_util.dumps(dct, default=json_util.default)
Expand Down