Skip to content

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

Merged
merged 3 commits into from
Sep 10, 2020

Conversation

prashantmital
Copy link
Contributor

No description provided.

@prashantmital
Copy link
Contributor Author

I took the opportunity to resync all bson corpus spec tests.

decode_extjson(parse_error_case['string'])
raise AssertionError('exception not raised for test '
'case: ' + description)
except (AttributeError, ValueError):
Copy link
Contributor Author

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

@@ -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,))
Copy link
Contributor Author

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

@prashantmital
Copy link
Contributor Author

@ShaneHarvey ready for another look.

@prashantmital prashantmital merged commit dc94ca6 into mongodb:master Sep 10, 2020
@prashantmital prashantmital deleted the SPEC-1582/uuid-extjson branch September 10, 2020 00:24
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.

2 participants