Skip to content

PYTHON-2883 Regex decoding error tests in top.json have unexpected, invalid syntax #721

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 6 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bson/json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,8 @@ def _parse_canonical_code(doc):
def _parse_canonical_regex(doc):
"""Decode a JSON regex to bson.regex.Regex."""
regex = doc['$regularExpression']
if not isinstance(regex['options'], str):
raise TypeError('Bad $regularExpression options, options must be string')
Copy link
Member

Choose a reason for hiding this comment

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

Please move this check to below the len(doc) and len(regex) checks to ensure we give good error messages in all cases. For the same reason can you also add the type() of regex['options'] to the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

if len(doc) != 1:
raise TypeError('Bad $regularExpression, extra field(s): %s' % (doc,))
if len(regex) != 2:
Expand Down
4 changes: 2 additions & 2 deletions test/bson_corpus/top.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@
},
{
"description": "Bad $regularExpression (pattern is number, not string)",
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": 42, \"$options\" : \"\"}}}"
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": 42, \"options\" : \"\"}}}"
},
{
"description": "Bad $regularExpression (options are number, not string)",
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": \"a\", \"$options\" : 0}}}"
"string": "{\"x\" : {\"$regularExpression\" : { \"pattern\": \"a\", \"options\" : 0}}}"
},
{
"description" : "Bad $regularExpression (missing pattern field)",
Expand Down