FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix Python BSON Binary and Code round-trip by xiaolu-ai26 · Pull Request #21 · onenodehq/onenode · GitHub

Fix Python BSON Binary and Code round-trip - #21

Open
xiaolu-ai26 wants to merge 1 commit into
onenodehq:mainfrom
xiaolu-ai26:fix-py-bson-roundtrip
Open

Fix Python BSON Binary and Code round-trip#21
xiaolu-ai26 wants to merge 1 commit into
onenodehq:mainfrom
xiaolu-ai26:fix-py-bson-roundtrip

Conversation

Copy link
Copy Markdown

Summary

Fixes Python SDK BSON round-trip handling for Binary and Code values.

Bugs

Code is serialized as a plain string

PyMongo's bson.Code is a subclass of str. Collection.__serialize() currently checks primitive strings before looking up BSON_SERIALIZERS, so Code values never reach the existing Code: lambda v: {"$code": str(v)} serializer. They are sent as plain strings instead of BSON Code Extended JSON.

The deserializer also ignores $scope, so scoped Code values cannot round-trip.

Binary loses subtype

Binary values are currently serialized as only a hex string:

{"$binary": v.hex()}

Deserialization reconstructs them with the default subtype:

Binary(bytes.fromhex(value["$binary"]))

That silently changes UUID/vector/user-defined/etc. BSON binary values into generic subtype 0.

Fix

  • Handle Code before the primitive str branch.
  • Serialize Code as $code plus optional recursively serialized $scope.
  • Deserialize $scope back into Code(code, scope).
  • Serialize Binary as { data, subType }.
  • Deserialize the new object form while preserving subtype, and keep backward compatibility for the old $binary: "hex" shape.

Validation

  • Verified against mongodb/mongo-python-driver 4.11.3 source: Binary stores subtype, and Code subclasses str with an optional scope property.
  • Ran python3 -m py_compile onenode-py/onenode/_collection.py.
  • Ran git diff --check.
  • Ran a local stubbed Python validation covering:
    • current Code string-subclass ordering issue
    • $scope serialization/deserialization
    • Binary subtype 4 serialization
    • hex-string subtype 128 deserialization
    • legacy $binary: "hex" fallback to subtype 0

Full package tests were not run because this environment does not have pymongo/bson installed and shell network cannot reach package indexes.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL