[CSHARP-3132] Bson regexes Created: 11/Jun/20  Updated: 27/Oct/23  Resolved: 02/Jul/20

Status: Closed
Project: C# Driver
Component/s: BSON, Serialization
Affects Version/s: 2.10.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Tomáš Chroboček Assignee: Mikalai Mazurenka (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All environments



 Description   

Hello. When the regex is value of property in root document there is no problem. The problem seems to be just for nested properties

Here is a unit test.

        public static IEnumerable<object[]> TestData()
        {
            yield return new object[]
            {
                new BsonDocument
                {
                    new BsonElement("field", new BsonRegularExpression("pattern"))
                }
            };
            yield return new object[]
            {
                new BsonDocument
                {
                    new BsonElement("field", new BsonDocument
                    {
                        new BsonElement("$regex", new BsonRegularExpression("pattern"))
                    })
                }
            };
        }
 
        [Theory]
        [MemberData(nameof(TestData))]
        public void Test(BsonDocument document)
        {
            var json = document.ToJson();
            BsonSerializer.Deserialize<BsonDocument>(json);
        }



 Comments   
Comment by Mikalai Mazurenka (Inactive) [ 29/Jun/20 ]

Hello muhaha45@gmail.com

The second BsonDocument you provided is being deserialized with an exception because its structure mimics the special structures used for serialized JSON regular expressions:

For Extended JSON v1 (https://docs.mongodb.com/manual/reference/mongodb-extended-json-v1/#regular-expression)

{ "$regex" : "<sRegex>", "$options" : "<sOptions>" }

And for Extended JSON v2 (https://docs.mongodb.com/manual/reference/mongodb-extended-json/#bson.Regular-Expression)

{ "$regularExpression" : { "pattern" : "<regexPattern>", "options" : "<options>" } }

This would happen if you try to construct any special JSON format structures with BSON.

The document below would serialize to JSON and deserialize as expected:

new BsonDocument
{
    new BsonElement("field", new BsonDocument
    {
        new BsonElement("field", new BsonRegularExpression("pattern"))
    })
}

So the problem is not in the usage of nested properties.

If you would like to produce the Extended JSON v2 output, please use the code snipped below:

var document = new BsonDocument
{
    new BsonElement("field", new BsonRegularExpression("pattern"))
};
var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.CanonicalExtendedJson };
var json = document.ToJson(jsonSettings); // { "field" : { "$regularExpression" : { "pattern" : "pattern", "options" : "" } } }
BsonSerializer.Deserialize<BsonDocument>(json);

And for Extended JSON v1 (this is deprecated output mode, so you have to suppress the warning if you would like to use it):

var document = new BsonDocument
{
    new BsonElement("field", new BsonRegularExpression("pattern"))
};
var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
var json = document.ToJson(jsonSettings); // { "field" : { "$regex" : "pattern", "$options" : "" } }
BsonSerializer.Deserialize<BsonDocument>(json);

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