Skip to content

Commit

Permalink
MongoDB: Fix edge case when decoding MongoDB Extended JSON elements
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 22, 2024
1 parent 368b01f commit e9ddcff
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- MongoDB: Fixed edge case when decoding MongoDB Extended JSON elements

## 2024/09/19 v0.0.16
- MongoDB: Added `MongoDBFullLoadTranslator` and `MongoDBCrateDBConverter`
Expand Down
2 changes: 1 addition & 1 deletion src/commons_codec/transform/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def decode_value(self, value: t.Any) -> t.Any:
"""
if isinstance(value, dict):
# Decode item in BSON CANONICAL format.
if len(value) == 1:
if len(value) == 1 and next(iter(value)).startswith("$"):
return self.decode_canonical(value)

# Custom adjustments to compensate shape anomalies in source data.
Expand Down
6 changes: 4 additions & 2 deletions tests/transform/mongodb/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"canonical": {
"code_ascii": {"$code": "abab"},
"code_bytes": {"$code": "ab\u0000ab\u0000"},
"code_scope": {"$code": "abab", "$scope": {"x": {"$numberInt": 42}}},
"code_scope": {"$code": "abab", "$scope": {"x": {"$numberInt": "42"}}},
"date_iso8601": {"$date": "2015-09-23T10:32:42.33Z"},
"date_numberlong": {"$date": {"$numberLong": "1356351330000"}},
"dbref": {
Expand All @@ -78,6 +78,7 @@
],
"list_dict": [
{"id": "bar", "value": {"$date": "2015-09-24T10:32:42.33Z"}},
{"value": {"$date": "2015-09-24T10:32:42.33Z"}},
],
"list_int": [
{"$numberInt": "-2147483648"},
Expand Down Expand Up @@ -163,7 +164,7 @@
"code_scope": {
"$code": "abab",
"$scope": {
"x": {"$numberInt": 42},
"x": 42,
},
},
"date_iso8601": 1443004362000,
Expand All @@ -187,6 +188,7 @@
],
"list_dict": [
{"id": "bar", "value": 1443090762000},
{"value": 1443090762000},
],
"list_int": [
-2147483648,
Expand Down

0 comments on commit e9ddcff

Please sign in to comment.