[CDRIVER-3773] libbson rejects regular expressions with no options field Created: 10/Aug/20  Updated: 02/Jun/21  Resolved: 10/Mar/21

Status: Closed
Project: C Driver
Component/s: libbson
Affects Version/s: 1.17.0
Fix Version/s: 1.18.0, 1.18.0-alpha

Type: Improvement Priority: Minor - P4
Reporter: Julius Park (Inactive) Assignee: Benji Rewis (Inactive)
Resolution: Done Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

This originally appeared in a test failure in python-bsonjs.

This is the failing test: https://github.com/mongodb-labs/python-bsonjs/blob/2947fc596c76fa82e47b903785bf1411f1c9be05/test/test_bsonjs.py#L146-L149. It fails with these logs:

ERROR: test_regex (test.test_bsonjs.TestBsonjs)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/build/mongodb-labs/python-bsonjs/test/test_bsonjs.py", line 147, in test_regex
res = bsonjs_loads('{"r": {"$regex": "a*b"}}')['r']
File "/home/travis/build/mongodb-labs/python-bsonjs/test/test_bsonjs.py", line 63, in bsonjs_loads
return to_object(bsonjs.loads(json_str))
ValueError: Missing "$options" after "$regex"

This specific test is loading legacy regex syntax without the $options field:

{"r": {"$regex": "a*b"}}

Though based on _bson_json_read_append_regex also rejects the canonical representation of a regex if the options field was missing, like:

{"r": {"$regularExpression": { "pattern": "a*b" }}

If libbson is passing the BSON corpus tests, I imagine it is not required by the spec that we permit regular expressions without options field. But if other tools may produce this (as the comment in test_bsonjs.py says), it seems worthwhile to not reject.

For more info about regex representation in JSON, see the extended JSON spec: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst



 Comments   
Comment by Benji Rewis (Inactive) [ 11/Mar/21 ]

As of the commit above, we now support legacy regexes with no options, but not canonical regexes with no options.

Comment by Githook User [ 11/Mar/21 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: CDRIVER-3773 Remove support for canonical regular expressions with no options (#754)
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/27f1d6e1be7eb85545ef1ebb14920434ea98d031

Comment by Shane Harvey [ 11/Mar/21 ]

Benji and I spoke about this ticket and came to the realization that we're only asking for the legacy $regex behavior to be changed here, not the canonical $regularExpression format. So can we un-revert this change for $regex?

Comment by Benji Rewis (Inactive) [ 11/Mar/21 ]

We cannot support regular expressions with no options without a spec change because of an existing spec test that checks for a parse error on a regex with no options.

This ticket would require a larger spec change, which I will look into. For now, I'm reverting the change through this PR.

Comment by Githook User [ 11/Mar/21 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: Revert "CDRIVER-3773 Support regular expressions with no options (#751)"

This reverts commit 2e685d183a8d5670d439e40059a34327daed84c7.
Branch: revert-751-allowNoOptionsRegex.3773
https://github.com/mongodb/mongo-c-driver/commit/152f26563835c4626fb2ac2c0b30b4dfc12965bd

Comment by Benji Rewis (Inactive) [ 10/Mar/21 ]

_bson_json_read_append_regex now supports regular expressions without options in both the legacy and canonical formats.

Comment by Githook User [ 10/Mar/21 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: CDRIVER-3773 Support regular expressions with no options (#751)
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/2e685d183a8d5670d439e40059a34327daed84c7

Comment by Benji Rewis (Inactive) [ 09/Mar/21 ]

https://github.com/mongodb/mongo-c-driver/pull/751

Generated at Wed Feb 07 21:18:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.