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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
That silently changes UUID/vector/user-defined/etc. BSON binary values into generic subtype 0.
Fix
Validation
Full package tests were not run because this environment does not have pymongo/bson installed and shell network cannot reach package indexes.