-
Notifications
You must be signed in to change notification settings - Fork 49
Add support for bson.dbref.DBRef
#746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe changes extend the JSON encoding and decoding functionality to support BSON Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant MontyEncoder
participant BSONLibrary
Client->>MontyEncoder: Serialize DBRef instance
MontyEncoder->>BSONLibrary: Check instance type (DBRef?)
BSONLibrary-->>MontyEncoder: Confirm DBRef type
MontyEncoder->>Client: Return dictionary with module, class, and DBRef fields
sequenceDiagram
participant Client
participant MontyDecoder
participant BSONLibrary
Client->>MontyDecoder: Deserialize JSON dictionary
MontyDecoder->>BSONLibrary: Verify dictionary structure for DBRef
BSONLibrary-->>MontyDecoder: Confirm DBRef representation
MontyDecoder->>Client: Return reconstructed DBRef instance
Possibly related PRs
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
Documentation on https://www.mongodb.com/docs/manual/reference/database-references/#format and in https://pymongo.readthedocs.io/en/stable/api/bson/dbref.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/monty/json.py (2)
633-645: Consider compatibility with older Python versions.The implementation uses the dictionary merge operator (
|) introduced in Python 3.9. This might cause compatibility issues if the library needs to support older versions.- return { - "@module": "bson.dbref", - "@class": "DBRef", - } | o.as_doc() + d = { + "@module": "bson.dbref", + "@class": "DBRef", + } + d.update(o.as_doc()) + return d
859-876: Good implementation of DBRef deserialization.The implementation correctly handles the DBRef deserialization, including handling of the optional database field and arbitrary extra fields that DBRef can contain beyond the standard fields.
Consider adding a brief comment explaining why DBRef needs special handling for extra fields, as this is different from how most other types are handled:
# DBRef is special because it can contain arbitrary additional fields # beyond the standard $ref, $id, and $db fields
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/monty/json.py(6 hunks)tests/test_json.py(2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
tests/test_json.py
772-772: Local variable djson is assigned to but never used
Remove assignment to unused variable djson
(F841)
⏰ Context from checks skipped due to timeout of 90000ms (5)
- GitHub Check: build (windows-latest, 3.13)
- GitHub Check: build (windows-latest, 3.12)
- GitHub Check: build (windows-latest, 3.11)
- GitHub Check: build (windows-latest, 3.10)
- GitHub Check: build (windows-latest, 3.9)
🔇 Additional comments (5)
tests/test_json.py (1)
51-54: Good handling of the optional dependency.The import of
DBRefis properly wrapped in a try-except block to gracefully handle situations where the bson library is not installed.src/monty/json.py (4)
531-531: LGTM!The docstring has been updated to correctly reflect the addition of DBRef support.
755-756: LGTM!Correctly added 'bson.dbref' to the list of special modules handled by the decoder.
936-937: LGTM!The documentation for jsanitize has been updated to mention DBRef support.
956-957: LGTM!Properly added DBRef to the list of bson types that can be preserved when allow_bson=True.
| @pytest.mark.skipif(DBRef is None, reason="bson not present") | ||
| def test_dbref(self): | ||
| dbref = DBRef( | ||
| "my_collection", 1, database="my_database", extra_field="extra_value" | ||
| ) | ||
| with pytest.raises(TypeError): | ||
| json.dumps(dbref) | ||
| djson = json.dumps(dbref, cls=MontyEncoder) | ||
| x = json.loads(dbref, cls=MontyDecoder) | ||
| assert isinstance(x, DBRef) | ||
| assert x.collection == "my_collection" | ||
| assert x.database == "my_database" | ||
| assert x.extra_field == "extra_value" | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the JSON deserialization parameter in the test_dbref method.
There's a bug in the test implementation. Line 773 uses dbref (the object) as the input to json.loads() instead of djson (the serialized string).
Apply this fix:
- x = json.loads(dbref, cls=MontyDecoder)
+ x = json.loads(djson, cls=MontyDecoder)Also, consider adding more assertions to verify that all properties match the original object, similar to how other tests in this file are structured.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| @pytest.mark.skipif(DBRef is None, reason="bson not present") | |
| def test_dbref(self): | |
| dbref = DBRef( | |
| "my_collection", 1, database="my_database", extra_field="extra_value" | |
| ) | |
| with pytest.raises(TypeError): | |
| json.dumps(dbref) | |
| djson = json.dumps(dbref, cls=MontyEncoder) | |
| x = json.loads(dbref, cls=MontyDecoder) | |
| assert isinstance(x, DBRef) | |
| assert x.collection == "my_collection" | |
| assert x.database == "my_database" | |
| assert x.extra_field == "extra_value" | |
| @pytest.mark.skipif(DBRef is None, reason="bson not present") | |
| def test_dbref(self): | |
| dbref = DBRef( | |
| "my_collection", 1, database="my_database", extra_field="extra_value" | |
| ) | |
| with pytest.raises(TypeError): | |
| json.dumps(dbref) | |
| djson = json.dumps(dbref, cls=MontyEncoder) | |
| - x = json.loads(dbref, cls=MontyDecoder) | |
| + x = json.loads(djson, cls=MontyDecoder) | |
| assert isinstance(x, DBRef) | |
| assert x.collection == "my_collection" | |
| assert x.database == "my_database" | |
| assert x.extra_field == "extra_value" |
🧰 Tools
🪛 Ruff (0.8.2)
772-772: Local variable djson is assigned to but never used
Remove assignment to unused variable djson
(F841)
Summary
DBRefis a bson type which holds a pointer to a specific document in a MongoDB collection. This PR adds support for serializing and de-serializingDBRefobjects tomonty.Summary by CodeRabbit