Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented CSHARP-424. Circular references now throw a BsonSerializa…
…tionException which can be caught instead of a StackOverflowException. Default maximum serialization depth is 100, but can be set higher if required.
- Loading branch information
Robert Stam
committed
Apr 1, 2012
1 parent
1e667c4
commit d53ff25
Showing
8 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
BsonUnitTests/DefaultSerializer/Serializers/CircularReferencesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* Copyright 2010-2012 10gen Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using NUnit.Framework; | ||
|
||
using MongoDB.Bson; | ||
using MongoDB.Bson.IO; | ||
using MongoDB.Bson.Serialization; | ||
|
||
namespace MongoDB.BsonUnitTests.Serialization | ||
{ | ||
[TestFixture] | ||
public class CircularReferencesTests | ||
{ | ||
public class C | ||
{ | ||
public int X { get; set; } | ||
public C NestedDocument { get; set; } | ||
public BsonArray BsonArray { get; set; } | ||
} | ||
|
||
[Test] | ||
public void TestCircularBsonArray() | ||
{ | ||
// note: setting a breakpoint in this method will crash the debugger if the locals window is open | ||
// because it tries to display the value of array (presumably it's getting an internal stack overflow) | ||
var array = new BsonArray(); | ||
array.Add(array); | ||
var c1 = new C { X = 1, BsonArray = array }; | ||
Assert.Throws<BsonSerializationException>(() => { var json = c1.ToBson(); }); | ||
Assert.Throws<BsonSerializationException>(() => { var json = c1.ToBsonDocument(); }); | ||
Assert.Throws<BsonSerializationException>(() => { var json = c1.ToJson(); }); | ||
} | ||
|
||
[Test] | ||
public void TestCircularDocument() | ||
{ | ||
var c1 = new C { X = 1 }; | ||
c1.NestedDocument = c1; | ||
Assert.Throws<BsonSerializationException>(() => { var json = c1.ToBson(); }); | ||
Assert.Throws<BsonSerializationException>(() => { var json = c1.ToBsonDocument(); }); | ||
Assert.Throws<BsonSerializationException>(() => { var json = c1.ToJson(); }); | ||
} | ||
|
||
[Test] | ||
public void TestNoCircularReference() | ||
{ | ||
var c2 = new C { X = 2 }; | ||
var c1 = new C { X = 1, NestedDocument = c2 }; | ||
|
||
var json = c1.ToJson(); | ||
var expected = "{ 'X' : 1, 'NestedDocument' : { 'X' : 2, 'NestedDocument' : null, 'BsonArray' : null }, 'BsonArray' : null }".Replace("'", "\""); | ||
Assert.AreEqual(expected, json); | ||
|
||
var memoryWriter = new MemoryStream(); | ||
using (var writer = BsonWriter.Create(memoryWriter)) | ||
{ | ||
BsonSerializer.Serialize(writer, c1); | ||
Assert.AreEqual(0, writer.SerializationDepth); | ||
} | ||
|
||
var document = new BsonDocument(); | ||
using (var writer = BsonWriter.Create(document)) | ||
{ | ||
BsonSerializer.Serialize(writer, c1); | ||
Assert.AreEqual(0, writer.SerializationDepth); | ||
} | ||
|
||
var stringWriter = new StringWriter(); | ||
using (var writer = BsonWriter.Create(stringWriter)) | ||
{ | ||
BsonSerializer.Serialize(writer, c1); | ||
Assert.AreEqual(0, writer.SerializationDepth); | ||
} | ||
} | ||
} | ||
} |