Skip to content

Commit 8305dc1

Browse files
committed
improvements
1 parent 04d45fa commit 8305dc1

File tree

6 files changed

+134
-12
lines changed

6 files changed

+134
-12
lines changed

src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ private BsonDeserializationContext(
4444
_allowDuplicateElementNames = allowDuplicateElementNames;
4545
_dynamicArraySerializer = dynamicArraySerializer;
4646
_dynamicDocumentSerializer = dynamicDocumentSerializer;
47-
_domain = domain;
48-
// _domain = domain ?? BsonSerializer.DefaultDomain; //TODO We could do this here, but let's keep it as the previous line to catch errors.
47+
_domain = domain ?? BsonSerializer.DefaultDomain;
4948
}
5049

5150
// public properties

src/MongoDB.Bson/Serialization/BsonSerializationContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ private BsonSerializationContext(
3636
{
3737
_writer = writer;
3838
_isDynamicType = isDynamicType;
39-
_domain = domain;
40-
// _domain = domain ?? BsonSerializer.DefaultDomain; //TODO We could do this here, but let's keep it as the previous line to catch errors.
39+
_domain = domain ?? BsonSerializer.DefaultDomain;
4140
}
4241

4342
// public properties

src/MongoDB.Bson/Serialization/BsonSerializer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public static bool UseZeroIdChecker
6767
internal static ReaderWriterLockSlim ConfigLock => _domain.ConfigLock;
6868

6969
// public static methods
70+
71+
/// <summary>
72+
/// //TODO
73+
/// </summary>
74+
/// <returns></returns>
75+
public static IBsonSerializationDomain CreateDomain() => new BsonSerializationDomain();
76+
7077
/// <summary>
7178
/// Deserializes an object from a BsonDocument.
7279
/// </summary>

src/MongoDB.Driver/MongoClientSettings.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Collections.ObjectModel;
1919
using System.Linq;
2020
using System.Text;
21+
using MongoDB.Bson.Serialization;
2122
using MongoDB.Driver.Core.Compression;
2223
using MongoDB.Driver.Core.Configuration;
2324
using MongoDB.Driver.Core.Misc;
@@ -47,6 +48,7 @@ public class MongoClientSettings : IEquatable<MongoClientSettings>, IInheritable
4748
private TimeSpan _connectTimeout;
4849
private MongoCredential _credential;
4950
private bool _directConnection;
51+
private IBsonSerializationDomain _domain;
5052
private TimeSpan _heartbeatInterval;
5153
private TimeSpan _heartbeatTimeout;
5254
private bool _ipv6;
@@ -99,6 +101,7 @@ public MongoClientSettings()
99101
_compressors = new CompressorConfiguration[0];
100102
_connectTimeout = MongoDefaults.ConnectTimeout;
101103
_directConnection = false;
104+
_domain = null;
102105
_heartbeatInterval = ServerSettings.DefaultHeartbeatInterval;
103106
_heartbeatTimeout = ServerSettings.DefaultHeartbeatTimeout;
104107
_ipv6 = false;
@@ -264,6 +267,23 @@ public bool DirectConnection
264267
}
265268
}
266269

270+
/// <summary>
271+
/// //TODO
272+
/// </summary>
273+
/// <exception cref="InvalidOperationException"></exception>
274+
public IBsonSerializationDomain Domain
275+
{
276+
get
277+
{
278+
return _domain;
279+
}
280+
set
281+
{
282+
if (_isFrozen) { throw new InvalidOperationException("MongoClientSettings is frozen."); }
283+
_domain = value;
284+
}
285+
}
286+
267287
/// <summary>
268288
/// Gets a value indicating whether the settings have been frozen to prevent further changes.
269289
/// </summary>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Linq;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.Serialization;
19+
using MongoDB.Bson.Serialization.Attributes;
20+
using MongoDB.Bson.Serialization.Serializers;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests
24+
{
25+
public class MultipleRegistriesTests
26+
{
27+
[Fact]
28+
public void GeneralTest()
29+
{
30+
// {
31+
// var client = DriverTestConfiguration.CreateMongoClient(c => c.Domain = BsonSerializer.DefaultDomain);
32+
// var db = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
33+
// db.DropCollection(DriverTestConfiguration.CollectionNamespace.CollectionName);
34+
// var collection = db.GetCollection<Person>(DriverTestConfiguration.CollectionNamespace.CollectionName);
35+
// var bsonCollection =
36+
// db.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName);
37+
//
38+
// var person = new Person { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mario", Age = 24 };
39+
// collection.InsertOne(person);
40+
//
41+
// var retrieved = bsonCollection.FindSync("{}").ToList().Single();
42+
// var toString = retrieved.ToString();
43+
//
44+
// var expectedVal =
45+
// """{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "Name" : "Mario", "Age" : 24 }""";
46+
// Assert.Equal(expectedVal, toString);
47+
// }
48+
49+
{
50+
var customDomain = BsonSerializer.CreateDomain();
51+
customDomain.RegisterSerializer(new CustomStringSerializer());
52+
53+
var client = DriverTestConfiguration.CreateMongoClient(c => c.Domain = customDomain);
54+
var db = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
55+
db.DropCollection(DriverTestConfiguration.CollectionNamespace.CollectionName);
56+
var collection = db.GetCollection<Person>(DriverTestConfiguration.CollectionNamespace.CollectionName);
57+
var bsonCollection =
58+
db.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName);
59+
60+
var person = new Person { Id = ObjectId.Parse("6797b56bf5495bf53aa3078f"), Name = "Mario", Age = 24 };
61+
collection.InsertOne(person);
62+
63+
var retrieved = bsonCollection.FindSync("{}").ToList().Single();
64+
var toString = retrieved.ToString();
65+
66+
var expectedVal =
67+
"""{ "_id" : { "$oid" : "6797b56bf5495bf53aa3078f" }, "Name" : "Mariotest", "Age" : 24 }""";
68+
Assert.Equal(expectedVal, toString);
69+
}
70+
}
71+
72+
public class Person
73+
{
74+
[BsonId] public ObjectId Id { get; set; }
75+
public string Name { get; set; }
76+
public int Age { get; set; }
77+
}
78+
79+
public class CustomStringSerializer : SealedClassSerializerBase<string>
80+
{
81+
/// <inheritdoc/>
82+
public override int GetHashCode() => 0;
83+
84+
protected override string DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
85+
{
86+
var bsonReader = context.Reader;
87+
88+
var bsonType = bsonReader.GetCurrentBsonType();
89+
return bsonType switch
90+
{
91+
BsonType.String => bsonReader.ReadString(),
92+
BsonType.Symbol => bsonReader.ReadSymbol(),
93+
_ => throw CreateCannotDeserializeFromBsonTypeException(bsonType)
94+
};
95+
}
96+
97+
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args,
98+
string value)
99+
{
100+
var bsonWriter = context.Writer;
101+
bsonWriter.WriteString(value + "test");
102+
}
103+
}
104+
}
105+
}

tests/MongoDB.Driver.Tests/Specifications/uuid/prose-tests/ImplicitEncodingTests.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ public void Implicit_encoding_with_pyton_legacy_representation_should_work_as_ex
8282
binaryData.Bytes.Should().Equal(BsonUtils.ParseHexString("00112233445566778899aabbccddeeff"));
8383
}
8484

85-
public class Person
86-
{
87-
[BsonId]
88-
public ObjectId Id { get; set; }
89-
public string Name { get; set; }
90-
public int Age { get; set; }
91-
}
92-
9385
[Fact]
9486
public void Implicit_encoding_with_standard_representation_should_work_as_expected()
9587
{

0 commit comments

Comments
 (0)