[JAVA-3708] Java Driver doesn't handle regex pattern flags like CASE_INSENSITIVE Created: 22/Apr/20  Updated: 22/Apr/20  Resolved: 22/Apr/20

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: 3.8.2, 3.12.0, 3.12.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Tad Yeager Assignee: Ross Lawley
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-1426 PatternCodec should handle unsupporte... Closed

 Description   

com.mongodb.client.model.Filters.regex(String fieldName, Pattern pattern) doesn't respect options like CASE_INSENSITIVE. 

Pattern p = Pattern.compile("(.)" + name.toLowerCase() + "(.)", Pattern.CASE_INSENSITIVE);
FindIterable<MongoEvent> me = collection.find(Filters.regex("name", p));

if translated to this command:

'{ "find" : "test.test_account", "filter" : { "name" : { "$regularExpression" :

{ "pattern" : "(.*)s1lvcvky(.*)", "options" : "" }

} } }'

So, upgrading my driver wrapper broke everything because compiled Pattern regular expressions are no longer supported properly.  Driver wrapper doesn't support regex options, ie regex(String fieldName, String pattern, String options).

Only related bug I found had to do with Pattern codec tests.



 Comments   
Comment by Ross Lawley [ 22/Apr/20 ]

Hi tad.yeager@gmail.com,

 

Glad to hear you figured it out.

 

Ross

Comment by Tad Yeager [ 22/Apr/20 ]

My bad.  It's the driver wrapper/serialization layer, MongoJack. Iit's not serializing the Filter correctly.   Sorry about that, please close.

Thanks.

Comment by Jeffrey Yemin [ 22/Apr/20 ]

I attempted to reproduce this with the 3.12 driver, and wasn't successful. Here's the complete test program

import com.mongodb.MongoClientSettings;
import com.mongodb.client.model.Filters;
import org.bson.BsonDocument;
import org.bson.conversions.Bson;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;
 
import java.util.regex.Pattern;
 
public class JAVA3708 {
    public static void main(String[] args) {
        String name = "Jeff";
        Pattern p = Pattern.compile("(.)" + name.toLowerCase() + "(.)", Pattern.CASE_INSENSITIVE);
        Bson filter = Filters.regex("name", p);
 
        BsonDocument filterDocument = filter.toBsonDocument(BsonDocument.class, MongoClientSettings.getDefaultCodecRegistry());
 
        System.out.println(filterDocument.toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build()));
    }
}

It outputs:

{"name": {"$regularExpression": {"pattern": "(.)jeff(.)", "options": "i"}}}

It's not clear to me what's different in your scenario. Can you provide a complete program that reproduces the issue?

Comment by Jeffrey Yemin [ 22/Apr/20 ]

upgrading my driver wrapper broke everything

From which version of the driver did you upgrade? Also, I'm not sure what you mean by "driver wrapper". I'm assuming you just mean the driver but let me know if I'm mistaken.

Comment by Tad Yeager [ 22/Apr/20 ]

Maybe?

Comment by Tad Yeager [ 22/Apr/20 ]

related?  JAVA-1426

Generated at Thu Feb 08 09:00:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.