-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-2361 Support parsing as extended JSON representation for subtype 4 binary #483
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
PYTHON-2361 Support parsing as extended JSON representation for subtype 4 binary #483
Conversation
I took the opportunity to resync all bson corpus spec tests. |
test/test_bson_corpus.py
Outdated
decode_extjson(parse_error_case['string']) | ||
raise AssertionError('exception not raised for test ' | ||
'case: ' + description) | ||
except (AttributeError, ValueError): |
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.
The AttributeError
is raised when the $uuid
field contains a value of the incorrect type - in this case a document.
ERROR: test_binary (test.test_bson_corpus.TestBSONCorpus)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/pmital/Developer/mongo-python-driver/test/test_bson_corpus.py", line 210, in run_test
decode_extjson(parse_error_case['string'])
File "/Users/pmital/Developer/mongo-python-driver/bson/json_util.py", line 409, in loads
return json.loads(s, *args, **kwargs)
File "/Users/pmital/.pyenv/versions/3.7.5/lib/python3.7/json/__init__.py", line 361, in loads
return cls(**kw).decode(s)
File "/Users/pmital/.pyenv/versions/3.7.5/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/pmital/.pyenv/versions/3.7.5/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
File "/Users/pmital/Developer/mongo-python-driver/bson/json_util.py", line 408, in <lambda>
pairs, json_options)
File "/Users/pmital/Developer/mongo-python-driver/bson/json_util.py", line 428, in object_pairs_hook
return object_hook(json_options.document_class(pairs), json_options)
File "/Users/pmital/Developer/mongo-python-driver/bson/json_util.py", line 452, in object_hook
return _parse_legacy_uuid(dct, json_options)
File "/Users/pmital/Developer/mongo-python-driver/bson/json_util.py", line 494, in _parse_legacy_uuid
return uuid.UUID(doc["$uuid"])
File "/Users/pmital/.pyenv/versions/3.7.5/lib/python3.7/uuid.py", line 157, in __init__
hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'SON' object has no attribute 'replace'
Since this is a fairly hard to understand error message for a user, I think we should manually validate that the type is correct (and raise something more sensible like a TypeError). Would you agree?
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.
Agreed.
a57f5f6
to
8b35301
Compare
@@ -508,6 +508,8 @@ def _parse_legacy_uuid(doc, json_options): | |||
"""Decode a JSON legacy $uuid to Python UUID.""" | |||
if len(doc) != 1: | |||
raise TypeError('Bad $uuid, extra field(s): %s' % (doc,)) | |||
if not isinstance(doc["$uuid"], text_type): | |||
raise TypeError('$uuid must be a string: %s' % (doc,)) |
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.
Used the same format for the error message as $binary
base64
@ShaneHarvey ready for another look. |
No description provided.