Skip to content

Commit c623169

Browse files
committed
- PR comments + tests fixes
1 parent 23b6bd2 commit c623169

File tree

5 files changed

+53
-37
lines changed

5 files changed

+53
-37
lines changed

src/MongoDB.Bson/BsonExtensionMethods.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,41 @@ public static BsonDocument ToBsonDocument<TNominalType>(
131131
/// <param name="configurator">The serialization context configurator.</param>
132132
/// <param name="args">The serialization args.</param>
133133
/// <returns>A BsonDocument.</returns>
134-
/// <exception cref="System.ArgumentNullException">nominalType</exception>
135-
/// <exception cref="System.ArgumentException">serializer</exception>
136134
public static BsonDocument ToBsonDocument(
137135
this object obj,
138136
Type nominalType,
139137
IBsonSerializer serializer = null,
140138
Action<BsonSerializationContext.Builder> configurator = null,
139+
BsonSerializationArgs args = default(BsonSerializationArgs)) =>
140+
ToBsonDocument(obj, nominalType, BsonDocumentWriterSettings.Defaults, serializer, configurator, args);
141+
142+
internal static BsonDocument ToBsonDocument<TNominalType>(
143+
this TNominalType obj,
144+
BsonDocumentWriterSettings bsonDocumentWriterSettings,
145+
IBsonSerializer serializer = null,
146+
Action<BsonSerializationContext.Builder> configurator = null,
147+
BsonSerializationArgs args = default) =>
148+
ToBsonDocument(obj, typeof(TNominalType), bsonDocumentWriterSettings, serializer, configurator, args);
149+
150+
internal static BsonDocument ToBsonDocument(
151+
this object obj,
152+
Type nominalType,
153+
BsonDocumentWriterSettings bsonDocumentWriterSettings,
154+
IBsonSerializer serializer = null,
155+
Action<BsonSerializationContext.Builder> configurator = null,
141156
BsonSerializationArgs args = default(BsonSerializationArgs))
142157
{
143158
if (nominalType == null)
144159
{
145-
throw new ArgumentNullException("nominalType");
160+
throw new ArgumentNullException(nameof(nominalType));
146161
}
147162
args.SetOrValidateNominalType(nominalType, "nominalType");
148163

164+
if (bsonDocumentWriterSettings == null)
165+
{
166+
throw new ArgumentNullException(nameof(bsonDocumentWriterSettings));
167+
}
168+
149169
if (obj == null)
150170
{
151171
return null;
@@ -175,11 +195,11 @@ public static BsonDocument ToBsonDocument(
175195

176196
// otherwise serialize into a new BsonDocument
177197
var document = new BsonDocument();
178-
using (var bsonWriter = new BsonDocumentWriter(document))
179-
{
180-
var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
181-
serializer.Serialize(context, args, obj);
182-
}
198+
using var bsonWriter = new BsonDocumentWriter(document, bsonDocumentWriterSettings);
199+
200+
var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
201+
serializer.Serialize(context, args, obj);
202+
183203
return document;
184204
}
185205

tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,7 @@ public void Read_should_throw_when_subject_is_disposed()
599599
var count = 2;
600600
subject.Dispose();
601601

602-
#pragma warning disable CA2022
603-
Action action = () => subject.Read(buffer, offset, count);
604-
#pragma warning restore CA2022
602+
Action action = () => _ = subject.Read(buffer, offset, count);
605603

606604
action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("BsonStreamAdapter");
607605
}

tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,7 @@ public void Read_should_throw_when_buffer_is_null()
362362
{
363363
var subject = CreateSubject();
364364

365-
#pragma warning disable CA2022
366-
Action action = () => subject.Read(null, 0, 0);
367-
#pragma warning restore CA2022
365+
Action action = () => _ = subject.Read(null!, 0, 0);
368366

369367
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("buffer");
370368
}
@@ -381,9 +379,7 @@ public void Read_should_throw_when_count_is_out_of_range(int destinationSize, in
381379
var subject = CreateSubject();
382380
var destination = new byte[destinationSize];
383381

384-
#pragma warning disable CA2022
385-
Action action = () => subject.Read(destination, offset, count);
386-
#pragma warning restore CA2022
382+
Action action = () => _ = subject.Read(destination, offset, count);
387383

388384
action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
389385
}
@@ -398,9 +394,7 @@ public void Read_should_throw_when_offset_is_out_of_range(int destinationSize, i
398394
var subject = CreateSubject();
399395
var destination = new byte[destinationSize];
400396

401-
#pragma warning disable CA2022
402-
Action action = () => subject.Read(destination, offset, 0);
403-
#pragma warning restore CA2022
397+
Action action = () => _ = subject.Read(destination, offset, 0);
404398

405399
action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("offset");
406400
}
@@ -411,9 +405,7 @@ public void Read_should_throw_when_subject_is_disposed()
411405
var subject = CreateDisposedSubject();
412406
var destination = new byte[1];
413407

414-
#pragma warning disable CA2022
415-
Action action = () => subject.Read(destination, 0, 1);
416-
#pragma warning restore CA2022
408+
Action action = () => _ = subject.Read(destination, 0, 1);
417409

418410
action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteBufferStream");
419411
}

tests/MongoDB.Bson.Tests/Serialization/Serializers/CircularReferencesTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616
using System.IO;
17-
using MongoDB.Bson;
17+
using System.Runtime.InteropServices;
1818
using MongoDB.Bson.IO;
1919
using MongoDB.Bson.Serialization;
2020
using Xunit;
@@ -34,7 +34,7 @@ public class C
3434
public void TestCircularBsonArray()
3535
{
3636
// note: setting a breakpoint in this method will crash the debugger if the locals window is open
37-
// because it tries to display the value of array (presumably it's getting an internal stack overflow)
37+
// because it tries to display the value of an array (presumably it's getting an internal stack overflow)
3838
var array = new BsonArray();
3939
array.Add(array);
4040
var c1 = new C { X = 1, BsonArray = array };
@@ -46,11 +46,23 @@ public void TestCircularBsonArray()
4646
[Fact]
4747
public void TestCircularDocument()
4848
{
49+
var bsonWriterSettings = new BsonBinaryWriterSettings();
50+
var bsonDocumentWriterSettings = new BsonDocumentWriterSettings();
51+
var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell };
52+
53+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
54+
{
55+
// Stack size is smaller on OS, observed on NET10
56+
bsonWriterSettings.MaxSerializationDepth = 50;
57+
bsonDocumentWriterSettings.MaxSerializationDepth = 50;
58+
jsonWriterSettings.MaxSerializationDepth = 50;
59+
}
60+
4961
var c1 = new C { X = 1 };
5062
c1.NestedDocument = c1;
51-
Assert.Throws<BsonSerializationException>(() => c1.ToBson());
52-
Assert.Throws<BsonSerializationException>(() => c1.ToBsonDocument());
53-
Assert.Throws<BsonSerializationException>(() => c1.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }));
63+
Assert.Throws<BsonSerializationException>(() => c1.ToBson(writerSettings: bsonWriterSettings));
64+
Assert.Throws<BsonSerializationException>(() => c1.ToBsonDocument(bsonDocumentWriterSettings: bsonDocumentWriterSettings));
65+
Assert.Throws<BsonSerializationException>(() => c1.ToJson(writerSettings: jsonWriterSettings));
5466
}
5567

5668
[Fact]

tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ public void Read_should_throw_when_buffer_is_null(
139139
{
140140
var subject = CreateSubject();
141141

142-
#pragma warning disable CA2022
143-
Action action = () => subject.Read(null, 0, 0);
144-
#pragma warning restore CA2022
142+
Action action = () => _ = subject.Read(null!, 0, 0);
145143

146144
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("buffer");
147145
}
@@ -173,9 +171,7 @@ public void Read_should_throw_when_count_is_invalid(int bufferLength, int offset
173171
}
174172
else
175173
{
176-
#pragma warning disable CA2022
177-
action = () => subject.Read(buffer, offset, count);
178-
#pragma warning restore CA2022
174+
action = () => _ = subject.Read(buffer, offset, count);
179175
}
180176

181177
action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
@@ -200,9 +196,7 @@ public void Read_should_throw_when_offset_is_invalid(int bufferLength, int offse
200196
}
201197
else
202198
{
203-
#pragma warning disable CA2022
204-
action = () => subject.Read(buffer, offset, 0);
205-
#pragma warning restore CA2022
199+
action = () => _ = subject.Read(buffer, offset, 0);
206200
}
207201

208202
action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("offset");

0 commit comments

Comments
 (0)