Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-1296: Add delayed lookup of child serializers to prevent stack…
… overflows when there are cyclic types.
- Loading branch information
Showing
22 changed files
with
396 additions
and
210 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
83 changes: 83 additions & 0 deletions
83
...oDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerTests.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,83 @@ | ||
/* Copyright 2015 MongoDB 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.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using FluentAssertions; | ||
using MongoDB.Bson.IO; | ||
using MongoDB.Bson.Serialization; | ||
using MongoDB.Bson.Serialization.Serializers; | ||
using NUnit.Framework; | ||
|
||
namespace MongoDB.Bson.Tests.Serialization.Serializers | ||
{ | ||
[TestFixture] | ||
public class EnumerableInterfaceImplementerSerializerTests | ||
{ | ||
public class C : IEnumerable<C> | ||
{ | ||
public int Id; | ||
public List<C> Children; | ||
|
||
public IEnumerator<C> GetEnumerator() | ||
{ | ||
return Children.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void LookupSerializer_should_not_throw_StackOverflowException() | ||
{ | ||
var serializer = BsonSerializer.LookupSerializer<C>(); | ||
|
||
serializer.Should().BeOfType<EnumerableInterfaceImplementerSerializer<C, C>>(); | ||
var itemSerializer = ((EnumerableInterfaceImplementerSerializer<C, C>)serializer).ItemSerializer; | ||
itemSerializer.Should().BeSameAs(serializer); | ||
} | ||
|
||
[Test] | ||
public void Serialize_should_return_expected_result() | ||
{ | ||
var subject = CreateSubject(); | ||
|
||
using (var stringWriter = new StringWriter()) | ||
using (var jsonWriter = new JsonWriter(stringWriter)) | ||
{ | ||
var context = BsonSerializationContext.CreateRoot(jsonWriter); | ||
var value = new C { Id = 1, Children = new List<C> { new C { Id = 2, Children = new List<C>() } } }; | ||
|
||
subject.Serialize(context, value); | ||
|
||
var json = stringWriter.ToString(); | ||
json.Should().Be("[[]]"); | ||
} | ||
} | ||
|
||
private IBsonSerializer<C> CreateSubject() | ||
{ | ||
// create subject without using the global serializer registry | ||
var serializerRegistry = new BsonSerializerRegistry(); | ||
var subject = new EnumerableInterfaceImplementerSerializer<C, C>(serializerRegistry); | ||
serializerRegistry.RegisterSerializer(typeof(C), subject); | ||
return subject; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.