diff --git a/bson/json_util.py b/bson/json_util.py index 6f32683b17..82c0653934 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -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() diff --git a/test/test_json_util.py b/test/test_json_util.py index fadcb431f6..195f1beac5 100644 --- a/test/test_json_util.py +++ b/test/test_json_util.py @@ -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)